Skip to content

Create PDFs With Recipe

Import Recipe from MuhammaraJS and create a new document with an output path. Recipe coordinates use a top-left origin, unlike the low-level API's PDF-native bottom-left origin.

var Recipe = require("@muhammara/native").Recipe;
var pdfDoc = new Recipe("new", "output.pdf", {
  version: 1.6,
  author: "John Doe",
  title: "A brand new PDF",
});

pdfDoc.createPage("letter").endPage().endPDF();

Pass an options object to set the PDF version and metadata. Pages accept named sizes such as letter and A4. Within a page, use chainable methods such as text, circle, polygon, rectangle, image, and comment. Call endPage() before creating or editing another page.

PDF Version

version sets the PDF level written into the file header. Recipe accepts the canonical decimal levels 1.0 through 1.7 and 2.0. Any other value falls back to 1.7 without an error, so check the header when a specific level matters.

var pdfDoc = new Recipe("new", "output.pdf", { version: 2.0 });

pdfDoc.createPage("letter").endPage().endPDF();
// output.pdf now starts with %PDF-2.0

The low-level createWriter API takes the integer enums instead; see Create A PDF.

Buffer Output

Pass Buffer.from("new") as the input and omit the output path to receive the completed PDF through the endPDF callback.

var pdfDoc = new Recipe(Buffer.from("new"));

pdfDoc
  .createPage("letter")
  .endPage()
  .endPDF(function (pdfBuffer) {
    // pdfBuffer is a Buffer containing the PDF.
  });

endPDF() does not return the Buffer; consume it in the callback.