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

Argument

Description

srcDirectoryId

id used to identify a source folder in the server file system where the PDF signed file to read has been stored

destDirectoryId

id used to identify a folder in the server file system when the extracted document (the certificate) will be saved

fileName

file name inside the specified folder

obj

embedded document, expressed as a javascript object

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

Argument

Description

fileName

CSV file name to create within the specified directory

directoryId

directory identifier, used to define the absolute path, in the central server, where the file will be stored; if null, there must be one only entry for this application

additionalSettings

optional javascript object, containing additional settings to define the PDF rendering, in terms of margins, page format (A4, Letter, etc.), line height, font name/size, etc.

{

fontName: "Courier",

fontSize: 10,

size: "A4",

top: 20, // top margin

left: 20, // left margin

bottom: 20, // bottom margin

height: 10, // line height

orientation: "portrate",

overwriteFile: true|false

}

Allowed values for fontName: "Courier-Bold", "Courier-BoldOblique", "Times-Roman", "Helvetica-Oblique", "Courier-Oblique", "Symbol", "Times-Italic", "Helvetica", "Helvetica-Bold", "Times-BoldItalic", "ZapfDingbats", "Times-Bold", "Helvetica-BoldOblique", "Courier"

Allowed values for orientation: "portrait","landscape"

Allowed values for size: "A0"..."A6","LETTER"

If the "overwriteFile" attribute is omitted, the specified file is always created from scratch and an already existing file would be deleted before. If set to false, the already existing file is appended and the "addPageToPDFFile" method will always append pages at the end.

fileId

a text id, representing the output stream, to refer in the next two methods.

Syntax for addLineToPDFFile method

utils.addLineToPDFFile(fileId, line, additionalSettings)

Details

Argument

Description

fileId

a text id, representing the output stream, needed to work qwith the correct output stream

line

String text to append to the current page; a new page is automatically created when the next line position would be over the max page height - bottom margin - line height. Page height depends on the page "size". It is possible to force a new page before reaching the bottom part of the page through the "addPageToPDFFile" method.

additionalSettings

additional settings for the current line, expressed as a javascript object; not supported at the moment

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

Argument

Description

fileId

a text id, representing the output stream, needed to work qwith the correct output stream

fontAlias

the font name: can be anything; it will be referred within the setLineToPDFFile method

fontFileName

path + font file name

Syntax for setLineToPDFFile method

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

Details

Argument

Description

fileId

a text id, representing the output stream, needed to work qwith the correct output stream

x

X coordinate where positioning the text

y

Y coordinate where positioning the text

line

String text to include to the current page, at the specified position

additionalSettings

mandatory settings for the current line, expressed as a javascript object, having the following optional attributes:

{

fontName: "Helvetica-Bold",

fontSize: 50

}

Allowed values for "fontName": "Courier-Bold", "Courier-BoldOblique", "Times-Roman", "Helvetica-Oblique", "Courier-Oblique", "Symbol", "Times-Italic", "Helvetica", "Helvetica-Bold", "Times-BoldItalic", "ZapfDingbats", "Times-Bold", "Helvetica-BoldOblique", "Courier"

Additionally, you can register external TTF fonts, using the previous method, loadPDFFont and then refer them here: fontName will be set as the fontAlias in the previous method

Syntax for addImageToPDFFile method

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

Details

Argument

Description

fileId

a text id, representing the output stream, needed to work qwith the correct output stream

x

X coordinate where positioning the image

y

Y coordinate where positioning the image

imgPath

absolute path + file name of the image to include; image must be locally saved and must have one of these formats: png, jpeg, tif

width

image witdh; it must be already known

height

imahe height; it must be already known

Syntax for addBarcodeToPDFFile method

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

Details

Argument

Description

fileId

a text id, representing the output stream, needed to work qwith the correct output stream

x

X coordinate where positioning the barcode

y

Y coordinate where positioning the barcode

barcode

barcode, expressed as a String

barcodeType

the barcode type; supported types are: "Code128", "Code39", "Interleaved2Of5", "Ean8", "Upca", "pdf417" (case insensitive)

additionalSettings

mandatory settings for the current barcode, expressed as a javascript object, having the following optional attributes:

{

fontName: "Helvetica-Bold",

fontSize: 50,

textPosition: "NONE",

width: 200,

height: 100,

dpi: 300

}

Allowed values for "textPosition": "NONE" (do not show the label), "TOP" (show the label above the barocode), "BOTTOM" (show the label below the barcode)

Allowed values for "fontName": "Courier-Bold", "Courier-BoldOblique", "Times-Roman", "Helvetica-Oblique", "Courier-Oblique", "Symbol", "Times-Italic", "Helvetica", "Helvetica-Bold", "Times-BoldItalic", "ZapfDingbats", "Times-Bold", "Helvetica-BoldOblique", "Courier"

Syntax for closePDFFile method

var fileId = utils.closePDFFile(fileId)

Details

Argument

Description

fileId

a text id, representing the output stream, needed to work with the correct output stream

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

Argument

Description

fileId

a text id, representing the output stream, needed to work with the correct output stream

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

Argument

Description

fileId

a text id, representing the output stream, needed to work with the correct output stream

pageIndex

page index, starting from 0

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

Argument

Description

fileId

a text id, representing the output stream, needed to work with the correct output stream

total number of pages

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

Argument

Description

fileId

a text id, representing the output stream, needed to work with the correct output stream

it returns either "portrait" or "landscape"

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

Argument

Description

dirId

a directory id, where the already existing PDF file is stored

pdfFile

PDF file name

background

text to include in the background, for each page

settings

additional settings, expressed as a javascript object, having the following optional attributes:

{

red: 200,

green: 200,

blue: 200,

fontName: "Helvetica-Bold",

fontSize: 50,

fileName: "newfile.pdf"

}

Allowed values for "fontName": "Courier-Bold", "Courier-BoldOblique", "Times-Roman", "Helvetica-Oblique", "Courier-Oblique", "Symbol", "Times-Italic", "Helvetica", "Helvetica-Bold", "Times-BoldItalic", "ZapfDingbats", "Times-Bold", "Helvetica-BoldOblique", "Courier"

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

Argument

Description

passwordRestriction

password for the read-only user

passwordOwner

password for the document owner

fullPathSrc

absolute path + PDF file name for the already existing PDF file to protect

fullPathDest

absolute path + PDF file name for the PDF file protected

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

destinationFile

absolute path + file name of the PDF file to create, in the local file system

files

array of already existing PDF files to append; each file is a String composed of absolute path + file name

Last updated