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 (available under Windows only)

Colors are specified as RGBA numbers in the format 0xRRGGBBAA, e.g. 0x000000FF for opaque black or 0xFF000080 for semi-transparent red.

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');
myPdf.free();

Properties


fill

Type: boolean

Specifies whether to fill with fillColor the shapes you create with lineTo() and bezierTo(). Takes effect when createPath() is called. Default: false.


fillColor

Type: number

Specifies the color (0xRRGGBBAA) to fill in shapes, see fill. Takes effect when createPath() is called. Default: 0x000000FF (black).


font

Type: string

Specifies the name of the font to use with addText(). Default: 'Arial'.


fontAlign

Type: infinity.pdf.align

Specifies the alignment of text created by addText(), according to align. Default: infinity.pdf.align.left.


fontColor

Type: number

Specifies the color (0xRRGGBBAA) of the text created by addText(). Default: 0x000000FF (black).


fontSize

Type: number

Specifies the size of the text created by addText(). Default: 12.


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 the values 0, 90, 180 and 270 are supported; any other value sets the rotation to 0.


password

Type: string

Gets or sets the pdf-file password (e.g. to open an encrypted document with loadFromFile()).


stroke

Type: boolean

Specifies whether to draw a stroke (before version 3.11 the getter returned 1/0 instead of a boolean) with operations like lineTo() and bezierTo() (the created shapes still can be filled with colors). Takes effect when createPath() is called. Default: true.


strokeColor

Type: number

Specifies the color (0xRRGGBBAA) of the stroke. Takes effect when createPath() is called. Default: 0x000000FF (black).


strokeWidth

Type: number

Specifies the width of the stroke. Takes effect when createPath() is called. Default: 1.


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 PDF version of the document ('1.0' to '1.7', or 'unknown').


Methods


constructor()

Lets you create an INFINITY.JS pdf file object instance containing a new, empty document. Doesn't accept parameters. Throws an error if the PDFium library (pdfium.dll) cannot be loaded or lacks required functions (before version 3.11 this ended the script).

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. The image formats supported by infinity.image can be used. Throws an error if the image cannot be loaded.

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. Only used if height is given too. Default: the width of the image in pixels.

  • height: number, optional

    The height for the embedded image. Only used if width is given too. Default: the height of the image in pixels.

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. The image formats supported by infinity.image can be used. Throws an error if the image cannot be loaded.

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. Only used if height is given too. Default: the width of the image in pixels.

  • height: number, optional

    The height for the embedded image. Only used if width is given too. Default: the height of the image in pixels.

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, using the current font, fontSize, fontColor and fontAlign.

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, optional

    The angle in degrees. Default: 0.

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.

  • 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 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. The current values of fill, fillColor, stroke, strokeColor and strokeWidth are used for the 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. The object must not be used afterwards: methods and properties that access the document then throw an error.

Signature:

free(): void

Example:

myPdf.free();

lineTo()

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

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 the given pdf-file, replacing the current document. Returns false if the file cannot be loaded (e.g. if it does not exist, is not a valid pdf-file or the password is wrong).

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:

if ( myPdf.loadFromFile('test.pdf') ) {
  console.debug(myPdf.pageCount);
}

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. The image format is determined by the file extension (e.g. .bmp, .png, .jpg). Throws an error if the image cannot be saved.

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 document as a pdf-file at the given location. Returns true on success.

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.

  • right: 1

    Align text to the right side of the document.

  • center: 2

    Align text to the center of the document.

Example:

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