Skip to content

infinity.pdf

Provides functionality for creating and formatting PDF (Portable Document Format) files. Supports pixel-perfect embedding of images, aligning text, drawing Bezier curves. Lets you rotate pages as well as render them to image files.

Module: infinity.pdf

Classes:
Enums:

Classes

pdf

The class for operations with PDF documents.

Example:

infinity.loadModule('infinity.pdf');
let myPdf = new infinity.pdf();

//adding the first page in the size of the DIN A4 format @72 PPI
myPdf.addPage(1, 595, 842);
myPdf.font = 'Arial';

//adding an image to the top right corner. Use only after you placed a corresponding image file at the specified location:
//myPdf.addImage(infinity.current.root + '../image.bmp', myPdf.pageWidth*0.88, myPdf.pageHeight*0.95, 20, 20);

//adding company name and document title
myPdf.fontAlign = infinity.pdf.align.center;
myPdf.fontColor = 0x000077FF;
myPdf.addText('Big Company', myPdf.pageWidth*0.855, myPdf.pageHeight*0.935);

myPdf.fontSize = 24;
myPdf.fontColor = 0x000000FF;
myPdf.addText('Invoice 21689', myPdf.pageWidth*0.5, myPdf.pageHeight*0.855);

//creating a dividing line under document title
myPdf.createPath(myPdf.pageWidth*0.2, myPdf.pageHeight*0.84);
myPdf.stroke = true;
myPdf.strokeColor = 0x000000FF;
myPdf.lineTo(myPdf.pageWidth*0.8, myPdf.pageHeight*0.84);
myPdf.addPath();

//creating a contrasting box at the bottom of the page
myPdf.fill = true;
myPdf.fillColor = 0xFFFF0033;
myPdf.strokeColor = 0xFF0000FF;
myPdf.createPath(myPdf.pageWidth*0.09, myPdf.pageHeight*0.30);
myPdf.lineTo(myPdf.pageWidth*0.90, myPdf.pageHeight*0.30);
myPdf.lineTo(myPdf.pageWidth*0.90, myPdf.pageHeight*0.23);
myPdf.lineTo(myPdf.pageWidth*0.09, myPdf.pageHeight*0.23);
myPdf.closePath();
myPdf.addPath();

//adding text to the box
myPdf.fontSize = 16;
myPdf.addText('Attention:', myPdf.pageWidth*0.17, myPdf.pageHeight*0.26);

//creating a signature at the bottom of the page
myPdf.fill = false;
myPdf.strokeColor = 0x000000FF;
myPdf.createPath(myPdf.pageWidth*0.2, myPdf.pageHeight*0.15);

myPdf.bezierTo(myPdf.pageWidth*0.1, myPdf.pageHeight*0.1, myPdf.pageWidth*0.2, myPdf.pageHeight*0.1, myPdf.pageWidth*0.3, myPdf.pageHeight*0.2);

myPdf.addPath();

//adding a rotated page to the document
myPdf.addPage(2, 595, 842);//DIN A4 @72 PPI
myPdf.pageRotation=90;

myPdf.saveToFile('test.pdf');

Properties


fill

Type: boolean

Specifies whether to fill with fillColor the shapes you create with lineTo() and bezierTo().


fillColor

Type: number

Specifies the color to fill in shapes, see fill.


font

Type: string

Specifies the font to use with addText().


fontAlign

Type: infinity.pdf.align

Specifies the alignment of text created by addText(), according to align.


fontColor

Type: number

Specifies the color of the text created by addText().


fontSize

Type: number

Specifies the size of the text created by addText().


pageNumber

Type: number

Gets or sets the current page to be worked on.


pageRotation

Type: number

Gets or sets the rotation of the current page in degrees. Only values like 90, 180, 270 lead to actual changes.


password

Type: string

Gets or sets the pdf-file password.


stroke

Type: boolean

Specifies whether to draw a stroke with operations like lineTo() and bezierTo() (the created shapes still can be filled with colors).


strokeColor

Type: number

Specifies the color of the next drawn stroke.


strokeWidth

Type: number

Specifies the width of the next drawn stroke.


author

Type: string

Gets the value of the author metadata field.


creator

Type: string

Gets the value of the creator metadata field.


pageCount

Type: number

Gets the total page count of the pdf document.


pageHeight

Type: number

Gets the height of the current page in pixels.


pageWidth

Type: number

Gets the width of the current page in pixels.


producer

Type: string

Gets the value of the producer metadata field.


subject

Type: string

Gets the value of the subject metadata field.


text

Type: string

Gets the text data contained inside the body of the pdf document.


title

Type: string

Gets the value of the title metadata field.


version

Type: string

Gets the value of the version metadata field.


Methods


constructor()

Lets you create an INFINITY.JS pdf file object instance. Doesn't accept parameters.

Signature:

constructor()

Example:

infinity.loadModule('infinity.pdf');
let myPdf = new infinity.pdf();

addImage()

Embeds an image out of the given stream into the current page of the pdf document at the specified coordinates.

Signature:

addImage( stream: infinity.stream, x: number, y: number, width?: number, height?: number ): void

Parameters:

  • stream: infinity.stream

    Stream object to receive image data from. See infinity.stream.

  • x: number

    The horizontal coordinate in pixels.

  • y: number

    The vertical coordinate in pixels.

  • width: number, optional

    The width for the embedded image.

  • height: number, optional

    The height for the embedded image.

Example:

myPdf.addImage(myStream, myPdf.pageWidth*0.88, myPdf.pageHeight*0.95);

addImage()

Embeds the given image file into the current page of the pdf document at the specified coordinates.

Signature:

addImage( fileName: string, x: number, y: number, width?: number, height?: number ): void

Parameters:

  • fileName: string

    Filename, relative path (location relative to the folder with the used INFINITY.JS executable file) or absolute path to the file which has to be loaded.

  • x: number

    The horizontal coordinate in pixels.

  • y: number

    The vertical coordinate in pixels.

  • width: number, optional

    The width for the embedded image.

  • height: number, optional

    The height for the embedded image.

Example:

myPdf.addImage(infinity.current.root + '../image.bmp', myPdf.pageWidth*0.88, myPdf.pageHeight*0.95, 20, 20);

addPage()

Adds a new page with the specified size to the current document and selects it for editing.

Signature:

addPage( pageNumber: number, width: number, height: number ): void

Parameters:

  • pageNumber: number

    The number for the new page. The sequence can be determined freely.

  • width: number

    The width for the new page in pixels.

  • height: number

    The height for the new page in pixels.

Example:

myPdf.addPage(2, 595, 842);//DIN A4 @72 PPI

addPath()

Adds the path previously created with createPath() and lineTo() or bezierTo() to the current page.

Signature:

addPath(): void

Example:

myPdf.createPath(myPdf.pageWidth*0.2, myPdf.pageHeight*0.84);
myPdf.stroke = true;
myPdf.strokeColor = 0x000000FF;
myPdf.lineTo(myPdf.pageWidth*0.8, myPdf.pageHeight*0.84);
myPdf.addPath();

addText()

Adds a row of text to the current page at the specified coordinates of the current page.

Signature:

addText( text: string, x: number, y: number, angle?: number ): void

Parameters:

  • text: string

    The text to add.

  • x: number

    The horizontal coordinate in pixels.

  • y: number

    The vertical coordinate in pixels.

  • angle: number

    The angle in degrees.

Example:

myPdf.fontAlign = infinity.pdf.align.center;
myPdf.fontColor = 0x000000FF;
myPdf.fontSize = 24;
myPdf.addText('Invoice 21689', myPdf.pageWidth*0.5, myPdf.pageHeight*0.85);

bezierTo()

Draws a Bezier curve to the specified coordinates of the current page.

Signature:

bezierTo( x1: number, y1: number, x2: number, y2: number, x3: number, y3: number ): void

Parameters:

  • x1: number

    The horizontal coordinate in pixels of the first control point used to specify the shape of the curve.

  • y1: number

    The vertical coordinate in pixels of the first control point used to specify the shape of the curve of the first control point used to specify the shape of the curve.

  • x2: number

    The horizontal coordinate in pixels of the second control point used to specify the shape of the curve.

  • y2: number

    The vertical coordinate in pixels of the first control point used to specify the shape of the curve of the second control point used to specify the shape of the curve.

  • x3: number

    The horizontal coordinate in pixels of the destination point for the end of the curve.

  • y3: number

    The vertical coordinate in pixels of the destination point for the end of the curve.

Example:

myPdf.createPath(myPdf.pageWidth*0.2, myPdf.pageHeight*0.15);

myPdf.bezierTo(myPdf.pageWidth*0.1, myPdf.pageHeight*0.1, myPdf.pageWidth*0.2, myPdf.pageHeight*0.1, myPdf.pageWidth*0.3, myPdf.pageHeight*0.2);

myPdf.addPath();

closePath()

Draws a line between the start and the end point of the path, thus closing it.

Signature:

closePath(): void

Example:

myPdf.createPath(myPdf.pageWidth*0.09, myPdf.pageHeight*0.30);
myPdf.lineTo(myPdf.pageWidth*0.90, myPdf.pageHeight*0.30);
myPdf.lineTo(myPdf.pageWidth*0.90, myPdf.pageHeight*0.23);
myPdf.lineTo(myPdf.pageWidth*0.09, myPdf.pageHeight*0.23);
myPdf.closePath();
myPdf.addPath();

createPath()

Sets a start point for a new path

Signature:

createPath( x: number, y: number ): void

Parameters:

  • x: number

    The horizontal coordinate in pixels.

  • y: number

    The vertical coordinate in pixels.

Example:

myPdf.createPath(myPdf.pageWidth*0.09, myPdf.pageHeight*0.30);

deletePage()

Deletes the specified page.

Signature:

deletePage( pageNumber: number ): void

Parameters:

  • pageNumber: number

    The number of the page to delete.

Example:

myPdf.deletePage(2);

free()

Frees the memory previously occupied by the pdf object instance.

Signature:

free(): void

Example:

myPdf.free();

lineTo()

Draws a straight line from the current point of the path to the specified coordninates.

Signature:

lineTo( x: number, y: number ): void

Parameters:

  • x: number

    The horizontal coordinate in pixels.

  • y: number

    The vertical coordinate in pixels.

Example:

myPdf.createPath(myPdf.pageWidth*0.2, myPdf.pageHeight*0.84);
myPdf.lineTo(myPdf.pageWidth*0.8, myPdf.pageHeight*0.84);

loadFromFile()

Loads data from the given pdf-file into memory.

Signature:

loadFromFile( fileName: string ): boolean

Parameters:

  • fileName: string

    Filename, relative path (location relative to the folder with the used INFINITY.JS executable file) or absolute path to the file to be loaded from.

Return type: boolean

Example:

myPdf.loadFromFile('test.pdf');

moveTo()

Moves the current point of the path to the specified coordinates.

Signature:

moveTo( x: number, y: number ): void

Parameters:

  • x: number

    The horizontal coordinate in pixels.

  • y: number

    The vertical coordinate in pixels.

Example:

myPdf.moveTo(myPdf.pageWidth*0.8, myPdf.pageHeight*0.84);

reloadPage()

Resets current pending changes to the page.

Signature:

reloadPage(): void

Example:

myPdf.reloadPage();

renderPage()

Renders the current page to an image file at the given location.

Signature:

renderPage( fileName: string, left: number, top: number, width: number, height: number ): void

Parameters:

  • fileName: string

    Filename, relative path (location relative to the folder with the used INFINITY.JS executable file) or absolute path to the file which has to be saved.

  • left: number

    The offset in pixels to the left image corner.

  • top: number

    The offset in pixels to the top image corner.

  • width: number

    The width of the final image.

  • height: number

    The height of the final image.

Example:

myPdf.renderPage('test.bmp', 0, 0, 595, 841);

saveToFile()

Saves the created work as a pdf-file at the given location.

Signature:

saveToFile( fileName: string ): boolean

Parameters:

  • fileName: string

    Filename, relative path (location relative to the folder with the used INFINITY.JS executable file) or absolute path to the file to save the data to.

Return type: boolean

Example:

myPdf.saveToFile('test.pdf');

updatePage()

Flushes the current page content. Make sure to call this method to ensure you don't loose any pending changes.

Signature:

updatePage(): void

Example:

myPdf.updatePage();

Enums

infinity.pdf.align

Values:

  • left: 0

    Align text to the left side of the document.

  • center: 1

    Align text to the center of the document.

  • right: 2

    Align text to the right side of the document.

Example:

myPdf.fontAlign = infinity.pdf.align.center;
myPdf.addText('Invoice 21689', myPdf.pageWidth*0.5, myPdf.pageHeight*0.85);