pdf()

Usage

pdf(page [, wkhtmltopdfParameter, baseUrl, runWithXServer, xServerSettings])

Description

Returns a PDF string representation of the given page.

Parameters

  • page: The page that should be rendered as a PDF
  • wkhtmltopdfParameter: This string is passed to wkhtmltopdf and may contain all parameters that wkhtmltopdf accepts
    • a useful parameter is --disable-smart-shrinking
  • baseUrl: The baseUrl for the main page (the header and footer page need the baseUrl explicitly as they are currently provided as a string)
    • defaults to the value of the keyword base_url
  • runWithXServer: Forces the usage of xvfb
  • xServerSettings: parameters for xvfb

Note

The PDF functionality relies on other software: wkhtmltopdf. This needs to be installed on the server.
It is recommended to install a wkhtmltopdf release from github to ensure that a version with patched qt is installed.
See the autogenerated documentation for wkhtmltopdf.

  • IMPORTANT: The dynamic file should have the charset ISO-8859-1 specified in its contentType (e.g. application/octet-stream; charset=ISO-8859-1)
  • IMPORTANT: If you are creating a PDF document from a dynamic file, make sure that there are no extraneous whitespaces after the dynamic script content. This will lead to corrupt PDFs and is very hard to detect!
  • If you experience caching issues, make sure that the dontCache flag of the file is set to true

Example

  1. Dynamic file which automatically downloads
	${{
		// download pdf file as "my-downloaded-file.pdf"
		Structr.setResponseHeader('Content-Disposition', 'attachment; filename="my-downloaded-file.pdf"');
		Structr.set_response_header('Cache-Control', 'no-cache');

		// These variables reference local pages in the structr installation
		let main   = 'pdf-export-main-page/';
		let header = '--header-html ' + Structr.get('base_url') + '/pdf-export-header-page/';
		let footer   = '--footer-html ' + Structr.get('base_url') + '/pdf-export-footer-page/';

		let wkhtmlArgs   = header + ' ' + footer + ' --disable-smart-shrinking';

		let pdf = Structr.pdf(main, wkhtmlArgs);

		Structr.print(pdf);
	}}
  1. Creates a new file for each run of the script:
	${set_content(create('File', 'name', 'new_document.pdf'), pdf('pdf-export-page'), 'ISO-8859-1')}

Info

When using page-based HTML headers and/or footers the following keys are appended to the request URL so they can be used directly in the page:

  • ${request.page} Replaced by the number of the pages currently being printed
  • ${request.frompage} Replaced by the number of the first page to be printed
  • ${request.topage} Replaced by the number of the last page to be printed
  • ${request.webpage} Replaced by the URL of the page being printed
  • ${request.section} Replaced by the name of the current section
  • ${request.subsection} Replaced by the name of the current subsection
  • ${request.date} Replaced by the current date in system local format
  • ${request.isodate} Replaced by the current date in ISO 8601 extended format
  • ${request.time} Replaced by the current time in system local format
  • ${request.title} Replaced by the title of the of the current page object
  • ${request.doctitle} Replaced by the title of the output document
  • ${request.sitepage} Replaced by the number of the page in the current site being converted
  • ${request.sitepages} Replaced by the number of pages in the current site being converted
About this article
Last change 2019-05-08
Topics FunctionsServerStructr 3.0