Skip to content

PdfBuilder

This module can be used to manipulate PDF files.

Manatee v1.29

This module requires Manatee v1.29 or later.

Create PDF from HTML

You can create a PDF file from HTML using the create function.

js
var pdfb = Module.load("PdfBuilder", {version: "vX.Y.Z"});
var doc = pdfb.create("<h1>Hello World</h1>");
doc.saveAs("/Users/robot/Desktop/hello.pdf");

Headers and footers

You can add headers and footers to the PDF file using the header and footer options.

js
var pdfb = Module.load("PdfBuilder", {version: "vX.Y.Z"});
var doc = pdfb.create(
    "<h1>Hello World</h1>", 
    { 
        header: { html: "<center><i>{page} of {total-pages}<i></center>"}, 
        footer: { html: "footer" } 
    });

Available options for the header and footer are:

  • html - The HTML to be used for the header or footer.
  • maxHeight - The max height of the header or footer.
  • drawDivider - Whether or not to draw a divider.
  • loadStyles - Use styles from the HTML document.

The header and footer HTML can contain the following placeholders:

  • {page}
  • {total-pages}
  • {url}
  • {date}
  • {time}
  • {html-title}
  • {pdf-title}

Forms

You can extract forms from a PDF file using the readForm function.

js
var pdfb = Module.load("PdfBuilder", {version: "vX.Y.Z"});
var doc = pdfb.open("/Users/robot/Desktop/form.pdf");
var form = doc.readForm();
// form is a JSON object where the keys are the field names and the values are the field values.

To input values into a form use the writeForm function supplying it with an object where the keys are the names of the fields and the values are the values to be written.

js
// Assuming we have a form with a field called "name" and a field called "email"
var myInputs = {
    "name": "John Doe",
    "email": "john@doe.org"
};
doc.writeForm(myInputs);
// Now, save the document with the new values.
doc.saveAs("/Users/robot/Desktop/form-filled.pdf");

It is also possible to supply a font and font size to the writeForm function.

javascript
var myInputs = {
"name": { text: "John Doe", font: "Courier", fontSize: 12 },
"email": "john@doe.org"
};
doc.writeForm(myInputs);

Flatten a PDF

Flattening a PDF file is a way to remove all annotations and other content that is not needed for the final document. It also makes all forms non-editable.

javascript
doc.flatten();

Merge PDFs

Using the merge function you can merge multiple PDF files into one.

js
doc.merge("/Users/robot/Desktop/second.pdf").merge("/Users/robot/Desktop/third.pdf");

or to merge an array of pdfs using the mergeAll function;

js
doc.mergeAll([
    "/Users/robot/Desktop/second.pdf",
    "/Users/robot/Desktop/third.pdf",
    "/Users/robot/Desktop/fourth.pdf"
]);

Split PDFs

Split a PDF file into multiple pages using the split function.

js
var docs = doc.split();
// Now, docs contain each page of the original document.
// You can save each page to a separate PDF file.
docs[0].saveAs("/Users/robot/Desktop/page1.pdf");

Print

Print a PDF file using the print function.

js
// Print to the default printer and show a preview.
doc.print({preview: true});
Releases
v2.0.0
  • Added header/footer support
v1.4.0
  • Added create PDF from HTML functionality
v1.3.0
  • Manatee v1.29 compatibility and upgrade dependencies
v1.2.2
  • Fix issue with release script (again)
v1.2.1
  • Fix issue with release script
v1.2.0
  • Update IronPdf dependency to 2022.4
  • Add flatten method
v1.1.1
  • Add support for specifying font and fontsize in writeForm function.
  • Cleanup temp files.
v1.1.0
  • Add support for http(s) urls in load function.
v1.0.0
  • Initial release.