HTML to PDF

Convert HTML content to PDF using wkhtmltopdf

Overview

This node converts HTML content or an HTML page URL into a PDF document using the wkhtmltopdf tool. It is useful for automating the generation of PDF reports, invoices, documentation, or any scenario where you need to transform web content into a portable and printable format. For example, you can convert a dynamically generated HTML invoice into a PDF file to send to customers, or archive a webpage snapshot as a PDF.

Properties

Name Meaning
HTML Content The raw HTML content to convert into a PDF.
HTML URL The URL of the HTML page to convert into a PDF.
Output Format How to return the generated PDF:
- Binary File (recommended)
- Base64 String
- File Path
Output Filename Custom filename for the PDF (without extension). If empty, an auto-generated name will be used.
Page Size Page size for the PDF. Options: A4, A3, A5, Letter, Legal
Orientation Page orientation. Options: Portrait, Landscape
Margin Top Top margin of the PDF page (e.g., 10mm, 1in, 20px)
Margin Right Right margin of the PDF page
Margin Bottom Bottom margin of the PDF page
Margin Left Left margin of the PDF page
Enable JavaScript Whether to enable JavaScript execution in the HTML before conversion
Custom Options Additional command-line options for wkhtmltopdf (space-separated), e.g., --disable-smart-shrinking --print-media-type
Custom wkhtmltopdf Path Custom path to the wkhtmltopdf binary. Leave empty for automatic detection.

Output

The output depends on the selected "Output Format":

  • Binary File: Outputs the PDF as a binary file attached to the item under the binary.pdf property with MIME type application/pdf. The filename and extension are set accordingly.
  • Base64 String: Outputs a JSON object containing the PDF encoded as a base64 string under the pdf field, along with its size and filename.
  • File Path: Saves the PDF to a temporary directory on disk and outputs a JSON object containing the file path, filename, and size.

Dependencies

  • Requires the external wkhtmltopdf binary installed on the system. The node attempts to auto-detect the binary location based on the operating system but allows specifying a custom path.
  • Uses Node.js modules: fs for file operations, path for file paths, and os for platform and temp directory info.
  • No special API keys or credentials are required.

Troubleshooting

  • Common issues:
    • wkhtmltopdf not found: The node cannot find the wkhtmltopdf executable. Ensure it is installed and accessible in one of the standard locations or specify a custom path.
    • Invalid input error: If neither HTML content nor HTML URL is provided, the node throws an error. Provide at least one.
    • Unknown output format: If an unsupported output format is selected, the node throws an error.
  • How to resolve:
    • Install wkhtmltopdf from https://wkhtmltopdf.org/downloads.html according to your OS.
    • Verify the binary path or provide a custom path in the node settings.
    • Always provide either HTML content or a valid URL.
    • Use supported output formats only.

Links and References

Discussion