· Updated

Generate PDF files using NodeJS and PDFMake

ASP.NET, PHP, Tutorials

This tutorial shows how to generate PDF files on the server with Node.js and PDFMake and call the PDF generator from a PHPRunner application. We use the same general approach for documents such as invoices, billing reminders and other PDFs that need to be created automatically on the server.

Before you start

You need:

  • access to the web server where the generated application runs;
  • permission to install and run Node.js on that server;
  • a folder in the PHPRunner project where the PDFMake script and its dependencies can be stored;
  • a writable location for the generated PDF files.

The PHP/PHPRunner part of the application passes the required values to the Node.js script. The Node.js script builds the document definition, generates the PDF and saves it to the location you specify.

1. Download Node.js and install it.

2. Inside your PHPRunner project create a folder named pdfmake.


3. Open the command line, navigate to the pdfmake folder and run:

This installs PDFMake and its dependencies. Create a file named index.js in the pdfmake folder and use the code below.

4. Here is the code we use to generate the PDF file:

5. Make sure the font files referenced above are available in the fonts folder and that the invoices_pdf folder is writable by the account running the web application.

6. Now we are ready to create PDF files. Here is how you execute the script from PHP code:

Using escapeshellarg() is important here because values such as addresses and product names may contain spaces.

And the same for C#:

[csharp]
dynamic param1 = “62384”;
dynamic param2 = “1600 Pennsylvania Avenue NW”;
dynamic param3 = “PHPRunner Enterprise Edition”;
dynamic param4 = “$999.00”;

CommonFunctions.exec((XVar)(MVCFunctions.Concat(
“\”c:/Program Files/nodejs/node.exe\” pdfmake/index.js “,
param1, ” “,
param2, ” “,
param3, ” “,
param4
)));
[/csharp]

Troubleshooting

If the command works from the command line but not from the web application, check which operating-system account is running the web server and whether that account is allowed to execute Node.js and write to the invoices_pdf folder.

If PDFMake cannot find a font file, verify the font path relative to the working directory used when the Node.js process starts.

It is also a good idea to test the Node.js command directly on the server with sample parameters before calling it from PHPRunner. That separates Node.js/PDFMake problems from application-integration problems.

1 thought on “Generate PDF files using NodeJS and PDFMake

  1. using iTextSharp.text;
    using iTextSharp.text.pdf;
    using System.IO;

    // Create a new PDF document
    Document document = new Document();

    // Create a PDF writer that writes the PDF document to a file stream
    PdfWriter writer = PdfWriter.GetInstance(document, new FileStream(“output.pdf”, FileMode.Create));

    // Open the PDF document for writing
    document.Open();

    // Add content to the PDF document
    Paragraph paragraph = new Paragraph(“Hello World!”);
    document.Add(paragraph);

    // Close the PDF document
    document.Close();
    In this example, we first import the necessary iTextSharp libraries. Then, we create a new Document object, which represents the PDF document we want to create.

    Next, we create a PdfWriter object, which is responsible for writing the PDF document to a file stream. We pass in the Document object and a new FileStream object that specifies the output file name and mode.

    We then open the Document object for writing, and add content to the PDF document using the Add method. In this example, we add a simple paragraph that says “Hello World!”

    Finally, we close the Document object to complete the PDF generation process. This will save the PDF file to the specified file stream.

    Note that this is a very basic example, and there are many more advanced features and options you can use when generating PDFs with iTextSharp.

Leave a Reply

Your email address will not be published. Required fields are marked *