Generating PDFs Dynamically

The Webvanta system can dynamically generate PDF files. (Note: this feature is available only on certain plans.)

The basic approach is to create a special HTML page, with corresponding CSS. This page can access the database using WebvantaScript, just like any other page. To generate the PDF, you call the JavaScript API with a pointer to the HTML page, and the system generates the PDF. You can then display a link to the PDF, or use the email API to send the PDF.

Creating Pages Suitable for PDF Generation

The PDF generator uses a version of the Webkit rendering engine, so it is capable of rendering HTML5 and CSS3 with high fidelity.

Pages must, however, be self-contained. The environment that a page is rendered in is not the same as a browser, so any state information is not available. This includes:

  • cookies
  • URL parameters
  • Anything else that depends on browser settings, history, or other data

All references must be absolute, including the protocol and domain name. In particular:

  • All external links
  • Links to CSS files
  • Links to images or other files in the CSS or the HTML
  • JavaScript src atributes

PDF Generation API

To generate a PDF, call the PDF generation JavaScript API by posting to the PDF endpoint. The endpoint is:

/admin/api/v2/pdf/<webvanta api key>/attach

The Webvanta API key is custom to each site. Contact support to get your key.

All of the information about the requested PDF is passed as a array parameter. The fields are:

  • path: Page to render into a PDF. You can optionally use the {{item}} substitute syntax to specify an item page.
  • item: Optional, ID of the item needed for the page if it is an item page
  • attach: hash describing where to attach the rendered PDF asset
    • cit_name: The CIT name
    • cit_field: The fieldname for the dedicated file field used to attach the PDF file
    • cit_id: The specific database item to which to attach this PDF

After calling the endpoint, you will receive back a standard Webvanta Job Status ID (jid) that you can use to poll for updates.

When the PDF generation has successfully completed, you will receive a data field on the status object that is a JSON array of tuples with asset info:

  • asset_id: Numeric id of PDF file
  • file_path: Full file path used to access the PDF file
  • content_type: The MIME type of the PDF file
  • filename: The base filename of the PDF file

Here's an example of PDF generation code:

  $.post("/admin/api/v2/pdf/2ccfecc0e8ed6ec70997cad406c60c0984d1e861/attach",
             {
               pdfs: [
                {path: "/certificate-print-out/{{item}}", 
                item: "1453541", 
                attach: {cit_name: "certificate",cit_field: 'pdf', cit_id: 1440810}
              ]
            },
              function(d, ts, jq) {
               // use the status object (jid) to set up progress and future handling
               if (d.status == "ok" && d.jid) {
                 progress(d.jid, "Generate PDF", "PDF processing", sendEmail);
               } else {
                 console.log("Something is wrong?", d, ts);
               }
             });

PDF Generation Parameters

The PDF generator supports the following optional parameters:

  • orientation
  • page_size
  • javascript_delay
  • grayscale
  • zoom
  • page_offset
  • page_width
  • page_height
  • dpi
  • margin_top
  • margin_bottom
  • margin_right
  • margin_left
  • no_background
  • print_media_type

Technical Document References

The following are references for the underlying code libraries used for this feature.