PDF File

Read a signed PDF file and extract data coming from the embedded certificate

The source PDF file is identified starting from the source directory id + fileName.

The specified PDF file is signed file, i.e. it contains a certificate; this certificate is then extracted and saved in the destination directory. The certificate file, if recognized with a MIME type, is renamed to a name having the original name + its real extension. Otherwise, it is saved with the original source file name.

Syntax

var obj = utils.getCertMessageFromPdf(Long srcDirectoryId,Long destDirectoryId,String fileName);

Details

The returned javascript object contains the following attributes:

  • expirationDate

  • firstName

  • lastName

  • personId

  • signerCountry

  • signerId

  • signerOrganization

  • personalVatNumber

  • corporateVatNumber

  • corporateName

  • personCountry

  • mimeType - MIME type for the embedded file; e.g. application/xml, application/pdf, etc.

  • destFilePath - absolute path + file name, related to the embedded file

  • signDate - the sign date, expressed as a javascript Date

Writing a very long PDF file on the server file system

From 6.0.2 version

Write a very long PDF file on the server file system, through a 3 steps approach:

  • first, open the output stream, through the "openPDFFile" method; it can work with already existing files as well, but in such a case, the memory consumption is higher and it is not recommended to read an already existing file if this is pretty big (hundreds pages or more).

  • next, add as many rows as you need, by invoking the "addLineToPDFFile" method multiple times, for each row to add

  • finally, close the output stream, by invoking the "closePDFFile"

Data could be retrieved from a SQL query, through the executeQueryWithCallback method, since this will ensure a limited amount of memory consumption.

Important note: the PDF file can be filled starting from a set of text lines; all lines inherit the same style (font + line height); lines are appended automatically, each with the same specified height; it is possibile to define the page size (e.g. A4, Letter, etc.), orientation (portrait vs landscape) and margins; a new page is automatically created when appended lines reach the bottom part of the current page.

This method can be used to create up to 1M lines (more or less some tens thousand pages), approximately in 15 mins, consuming about 200Mb of memory. Memory is upper-bound limited to such amount, since data is automatically flushed every 1000 pages.

Syntax for openPDFFile method

var fileId = utils.openPDFFile(
  fileName, 
  directoryId,
  additionalSettings
);

Details

Syntax for addLineToPDFFile method

utils.addLineToPDFFile(fileId, line, additionalSettings)

Details

Syntax for loadPDFFont method

utils.loadPDFFont(String fileId, String fontAlias,String fontFileName)

Use this method to register an external TTF font to use within this PDF, through setLineToPDFFile

Details

Syntax for setLineToPDFFile method

utils.setLineToPDFFile(fileId, x, y, line, additionalSettings)

Details

Syntax for addImageToPDFFile method

utils.addImageToPDFFile(String fileId,int x,int y,String imgPath,int width,int height)ettings)

Details

Syntax for addBarcodeToPDFFile method

utils.addBarcodeToPDFFile(
  String fileId,
  int x,
  int y,
  String barcode,
  String barcodeType,
  Map additionalSettings
);

Details

Syntax for closePDFFile method

var fileId = utils.closePDFFile(fileId)

Details

Example

// open the PDF file, in order to write into it
var fileId = utils.openPDFFile(
    "ab.pdf",
    9, // directory id
    {
      top: 20,
      left: 20,
      bottom: 20,
      fontName: "Courier",
      fontSize: 10,
      height: 10, 
      size: "A4",
      orientation: "portrait"
    } 
);
// if the additional settings are not specified, 
// a set of default values are used; the default values are as reported above

// declare a callback function, invoked by a SQL query, used to write a single line into the CSV file
var processRow = function(jsonRow) {
    utils.addLineToPDFFile(fileId,jsonRow.description);
}

// execute the SQL query, whose result set will be read row by row, by invoking each time the specified callback
utils.executeQueryWithCallback(
    "processRow", // callback function, invoked for each record coming from the query, whose argument will receive a JSON object representing the record
    "SELECT U.DESCRIPTION FROM PRM01_USERS U",
    null,
    false,
    true,
    []
);

// close the PDF file, after reading all records from the query
utils.closePDFFile(fileId);

Syntax for addPageToPDFFile method

It can be used to force the creation of a new page.

utils.addPageToPDFFile(fileId);

Details

Syntax for movePDFPage method

It can be used to move the cursor to the specified page (index starts from 0). Use this method only if an already existing file has been read (overwriteFile attribute set to false).

Only after invoking this method, the setLineToPDFPage can be invoked.

utils.movePDFPage(fileId,pageIndex);

Details

Syntax for getPDFPageCount method

It can be used to get the total number of pages in an already existing PDF document.

var nr = utils.getPDFPageCount(fileId);

Details

Syntax for getPDFOrientation method

It can be used to get the PDF page orientation, for an already existing PDF document.

var orientationString = utils.getPDFOrientation(fileId);

Details

Writing a background text to a PDF file

From 6.0.2 version

Starting from an already existing PDF file, this method adds a text in the middle of the page, for each page, in the background.

Syntax

utils.addBackgroundToPdf(
  Long dirId,
  String pdfFile,
  String background,
  Map settings
);

Details

Note: if no settings has been specified, the default values reported above will be used.

Protecting a PDF file

From 6.0.2 version

Starting from an already existing PDF file, this method defines the protection policy to add to a document for password-based protection: it protects a PDF document with password for the document owner and in read-only for the generic user

Syntax

utils.protectPdf(
  String passwordRestriction,
  String passwordOwner,
  String fullPathSrc,
  String fullPathDst
);

Details

Merging multiple PDF files

Starting from already existing PDF files, this method creates a new PDF files composed of all the other files, appended in the same order specified in the input list.

Syntax

utils.mergePdfs(String destinationFile,String[] files)

Details

Last updated