Skip to content

Sys.Pdf

Sys.Pdf works with PDF documents in server-side code. SimplePdf creates PDFs from HTML and opens existing PDFs for editing. The functions detect PDFs and scanned PDFs, render pages as PNG images, black out areas, and convert Word documents to PDF.

Functions that take BinaryOrBase64 accept a Buffer, ArrayBuffer, Uint8Array or Base64 string.

Availability: Server

isPdf(bufferOrBase64: Buffer | string): boolean

Checks whether the data is a PDF, by looking for the %PDF- header at its start.

ParameterTypeDescription
bufferOrBase64Buffer | stringThe data, as a Buffer or Base64 string.

Returns: boolean · Availability: Server

isScannedPdf(buffer: Buffer): boolean
isScannedPdf(bufferOrBase64: Buffer | FsBasePath, pathComponents?: string | string[]): Promise<boolean>

Checks whether a PDF has no text layer, as is typical for scanned documents. Lowgile treats a PDF as scanned when it contains no font object. Pass a Buffer to get the answer directly, or a file system base path and path components to check a file through Sys.Fs.

ParameterTypeDescription
bufferOrBase64Buffer | FsBasePathThe PDF as a Buffer, or the base path of a file, such as 'data' or 'tmp'. A Base64 string is not accepted.
pathComponentsstring | string[]Path of the file below the base path. Only used with a base path.

Returns: boolean for a Buffer, otherwise Promise<boolean> · Availability: Server

toPngImages(bufferOrBase64s: BinaryOrBase64 | BinaryOrBase64[], options?: PdfToImageOptions): Promise<Buffer<ArrayBufferLike>[]>

Renders every page of one or more PDFs as an image, using the pdf-to-img package (version ^6.2.0).

ParameterTypeDescription
bufferOrBase64sBinaryOrBase64 | BinaryOrBase64[]One PDF or a list of PDFs.
optionsPdfToImageOptionsRendering options.

PdfToImageOptions fields

FieldTypeRequiredDescription
format"png" | "jpg" | "jpeg"NoImage format. Default: png.
passwordstringNoPassword of an encrypted PDF.
scalenumberNoZoom factor; higher values give larger, sharper images. Default: 2.
renderParamsobjectNoFurther render parameters passed to pdf.js.
docInitParamsobjectNoFurther document parameters passed to pdf.js.

Returns: Promise<Buffer[]>, one image per page, the pages of all PDFs in order · Availability: Server

redactPdf(pdfData: BinaryOrBase64, redactionBoxes: RedactionBox[][]): Promise<Buffer>

Blacks out areas of a PDF and permanently removes the content beneath them, then removes the document metadata. The work is done by Lowgile Support Services, which must be configured for the instance (LOWGILE_SUPPORT_SERVICES_URL).

ParameterTypeDescription
pdfDataBinaryOrBase64The PDF.
redactionBoxesRedactionBox[][]One array of boxes per page, in page order. Pass an empty array for a page without redactions. The outer array must have one entry per page.

RedactionBox fields

Coordinates are percentages of the page width and height, from 0 to 100, measured from the top-left corner.

FieldTypeRequiredDescription
x1numberYesLeft edge.
y1numberYesTop edge.
x2numberYesRight edge.
y2numberYesBottom edge.

Returns: Promise<Buffer>, the redacted PDF · Availability: Server

convertDocxToPdf(docxData: BinaryDataOrFile): Promise<BinaryFile<Buffer>>

Converts a Word document to PDF. The conversion is done by Lowgile Support Services, which must be configured for the instance.

ParameterTypeDescription
docxDataBinaryDataOrFileThe document, either as binary data or as a file object with data, fileName and mimeType. The extension of fileName tells the converter the format; default: docx.

Returns: Promise<BinaryFile<Buffer>>: the PDF in data, the file name with the extension changed to .pdf in fileName (default Document.pdf), and application/pdf in mimeType · Availability: Server

Note

File and Blob objects are part of the declared BinaryData type, but the conversion throws an Unsupported input type error for them. The same applies to toPngImages() and redactPdf().

Creates and opens PDF documents. The documents it returns are pdf-lib PDFDocument objects (npm package pdf-lib, version ^1.17.1): you can add pages, draw text and images, fill forms, and save the result with save().

const doc = await Sys.Pdf.SimplePdf.createPdfFromHtml('<h1>Order confirmation</h1>', {
pageSize: 'A4',
orientation: 'Portrait',
})
const page = doc.getPages()[0]
page.drawText('Paid', { x: 50, y: 50, size: 24, color: Sys.Pdf.SimplePdf.Color.rgb(0, 0.5, 0) })
const pdfBytes = await doc.save()
createPdfFromHtml(html: string, options?: CreatePdfOptions): Promise<SimplePdfDocument>

Renders HTML to a PDF with wkhtmltopdf, which must be installed on the server, and opens the result for editing.

ParameterTypeDescription
htmlstringThe HTML document.
optionsCreatePdfOptionsPage size, margins, headers and footers and further wkhtmltopdf options.

Returns: Promise<SimplePdfDocument> · Availability: Server

createBlankPdf(options?: CreatePdfOptions): Promise<SimplePdfDocument>

Creates a PDF with one empty page, rendered by wkhtmltopdf with the given options.

ParameterTypeDescription
optionsCreatePdfOptionsPage size, margins and further wkhtmltopdf options.

Returns: Promise<SimplePdfDocument> · Availability: Server

openExistingPdf(buffer: ArrayBuffer | Buffer | string | Uint8Array): Promise<SimplePdfDocument>

Opens an existing PDF for reading and editing, with pdf-lib’s PDFDocument.load.

ParameterTypeDescription
bufferArrayBuffer | Buffer | string | Uint8ArrayThe PDF, as bytes or as a Base64 string.

Returns: Promise<SimplePdfDocument> · Availability: Server

const Fonts: typeof StandardFonts

The 14 standard PDF fonts, such as Helvetica or TimesRoman, for doc.embedFont(Sys.Pdf.SimplePdf.Fonts.Helvetica).

Type: typeof StandardFonts · Availability: Server

const Color: {
rgb: (red: number, green: number, blue: number) => RGB
cmyk: (cyan: number, magenta: number, yellow: number, key: number) => CMYK
grayscale: (gray: number) => Grayscale
}

Creates colours for drawing with pdf-lib. Every component is a number from 0 to 1. The colour types are available as SimplePdf.Color.Color, SimplePdf.Color.RGB, SimplePdf.Color.CMYK and SimplePdf.Color.Grayscale.

Type: object · Availability: Server

type CreatePdfOptions = Omit<Parameters<typeof WkHtmlToPdf>[1], (typeof ForbiddenWkOptions)[number]>

The options of the wkhtmltopdf npm package (version ^0.4.0), such as pageSize, orientation, marginTop or footerCenter. For security, options that give access to the server, run scripts or write files are not allowed: output, allow, cacheDir, disableJavascript, enableJavascript, disableLocalFileAccess, enableLocalFileAccess, disablePlugins, enablePlugins, postFile, runScript, sslCrtPath, sslKeyPath and sslKeyPassword. Lowgile also removes them at runtime.

Availability: Server

interface SimplePdfDocument extends Omit<PDFDocument, (typeof ForbiddenPdfDocumentMethods)[number]> {}

A pdf-lib PDFDocument without the addJavaScript method, which Lowgile blocks.

Availability: Server