Wire2PDF is the successor to and combination of the modules Pages2Pdf and WirePDF.
It provides a complete solution for generating PDF files in ProcessWire using the mPDF library. It supports:
- Automatic PDF generation from Pages via URL hooks (formerly Pages2Pdf).
- API usage to generate PDFs from HTML strings or files programmatically (formerly WirePDF).
- ProcessWire >= 3.0.0
- PHP >= 8.0
- Copy the module files to
/site/modules/Wire2PDF/. - Login to the ProcessWire admin and install the module Wire2PDF.
If you are migrating from the separate modules Pages2Pdf and WirePDF, follow these steps:
- Uninstall the old modules
Pages2PdfandWirePDF. - Install
Wire2PDF. - Templates: Your existing PDF templates in
/site/templates/pages2pdf/are fully compatible and will be used by Wire2PDF. You do not need to move them. - URL Parameters:
- The default GET variable has changed from
pages2pdftopdf. - Old URL:
/my-page/?pages2pdf=1 - New URL:
/my-page/?pdf=1 - Compatibility: If you want to keep your existing links working without changes, go to the module configuration of Wire2PDF and change the URL GET Variable setting back to
pages2pdf.
- The default GET variable has changed from
- API Usage:
- Search your code for
$modules->get('WirePDF')and replace it with$modules->get('Wire2PDF'). - The API methods (
save,download,markupMain, etc.) are compatible.
- Search your code for
The module configuration combines settings from both predecessors:
- Enabled templates: Select templates that can be converted to PDF.
- URL GET Variable: The query parameter to trigger generation (Default:
pdf). - Filename Pattern: e.g.,
{page.name}-pdf-{page.id}.pdf. - Creation Mode: Generate on click (Download) or on Page Save (Cache).
- Multilanguage: Generate separate PDFs for each language.
- Page Format/Orientation: A4, Letter, Portrait, Landscape, etc.
- Margins: Set default margins for content, header, and footer.
- Default Font/Size: Set the base typography.
- CSS File: Path to a global CSS file.
- PDF/A: Enable PDF/A-1b compliance.
This feature allows you to download a PDF of any page simply by appending a URL parameter.
- Enable the template of the page in the module settings.
- Create a PDF template in
/site/templates/pages2pdf/.default.php(Used if no specific template exists)basic-page.php(Specific to 'basic-page' template)_header.php/_footer.php(Global header/footer)styles.css(Global styles)
- Add a link to your site:
// Assuming default config 'getVar' => 'pdf'
echo "<a href='{$page->url}?pdf=1'>Download as PDF</a>";The render method takes an array of options that you can use to customize the output:
$options = array(
'title' => 'Print PDF',
'markup' => '<a href="{url}" target="_blank">{title}</a>',
'page_id' => '', // Pass a page ID if you want to download the PDF for another page
);
echo $modules->get('Wire2PDF')->render($options);You can also write the link to download a PDF file by yourself:
echo '<a href="' . $page->url . '?pdf=1">Download PDF</a>';
// Or to download a PDF file from another page
echo '<a href="' . $page->url . '?pdf=' . $pages->get('/my/page/')->id . '">Download PDF</a>';Depending on the chosen creation mode in the module configuration, the PDF file is cached before downloading or after saving a page. When the user requests a download of a PDF, the file is only re-created if the cache is expired or if debug mode is on.
Wire2PDF acts as a wrapper around the mPDF library. While it handles automatic PDF generation (see above), it can also be used independently via the API to create, store, or download PDF files programmatically.
The most important configuration options for mPDF are available in the module configs:
- Page orientation P for Portrait, L for Landscape
- Page format Format of the PDF file, A4,A3...
- Margins Left, Top, Right and Bottom margins of the document in mm
- Header margin top Margin of the header (top) in mm
- Footer margin bottom Margin of the footer (bottom) in mm
- Print header on first page Check to print the header also on the first page of a PDF file
- Default font Default font. Included font is: DejaVuSans
- Default font size Default font size in pt
- CSS file Path and filename of a CSS file containing default styles for the PDF HTML markup
- Author Author of the PDF
- Metadata Author, Title, Subject, Keywords, Creator
You can configure standard PDF metadata fields like Title, Subject, Keywords, Author, and Creator.
These fields support variable substitution using ProcessWire page fields:
- Standard variables:
{page.name},{page.id},{page.title} - Field variables: Any field from the page, e.g.,
{headline},{summary},{created}.
Note: HTML tags are automatically stripped, and HTML entities are decoded to ensure correct display of special characters (e.g., umlauts) in the PDF properties.
You can upload your own font files to be used in PDF generation.
- Go to Modules > Configure > Wire2PDF.
- Find the fieldset PDF Engine Settings (WirePDF/mPDF) and expand it.
- Inside, find the Font Settings section.
- Use the file input to select one or more
.ttffont files from your computer. - Click Save at the bottom of the page. The page will reload, and your fonts will be uploaded.
- The "Installed Custom Fonts" list will now show your uploaded font files.
To remove a custom font:
- In the "Installed Custom Fonts" list, check the trash icon next to the font you want to remove.
- Click Save.
Once a font is uploaded (e.g., MyCoolFont.ttf), it becomes available for use:
- The font name is derived from the filename, in lowercase and sanitized (e.g.,
mycoolfont). - You can select your custom font from the Default Font dropdown in the module configuration to make it the default for all PDFs.
- When using the API, you can set it dynamically:
$pdf->font = 'mycoolfont';Note: This feature currently only supports regular .ttf files. Bold, italic, or other variations will be treated as separate, regular fonts.
Here are some examples how you can create and store/download a PDF file:
$pdf = $modules->get('Wire2PDF');
// Define the main markup
$pdf->markupMain = $config->paths->templates . 'pdf_template.php';
// Header markup, header is only printed if you provide the markup
$pdf->markupHeader = $config->paths->templates . 'pdf_header.php';
// The same goes for the footer
$pdf->markupFooter = $config->paths->templates . 'pdf_footer.php';
// You can override any of the module config options if you have other needs, e.g.
$pdf->pageOrientation = 'L';
$pdf->pageFormat = 'A3';
$pdf->bottomMargin = 10;
// Saving the PDF file to disk
$pdf->save('/path/to/my-pdf-file.pdf');
// ... or request download
$pdf->download('pdf-filename.pdf');
// Setting the markup: Set path to a ProcessWire TemplateFile, an instance of a TemplateFile or just markup
// The lines below are equivalent
$pdf->markupMain = $config->paths->templates . 'pdf_template.php';
$pdf->markupMain = new TemplateFile($config->paths->templates . 'pdf_template.php');
$template = new TemplateFile($config->paths->templates . 'pdf_template.php');
$pdf->markupMain = $template->render();For advanced usage, you can also get the mPDF instance from the module and modify the object further:
$pdf = $modules->get('Wire2PDF');
// Get mPDF instance
$mpdf = $pdf->mpdf;You can also call mPDF methods directly on a Wire2PDF instance:
$pdf = $modules->get('Wire2PDF');
$pdf->markupMain = 'Hello World';
$pdf->author = 'John Doe';
$pdf->SetKeywords('ProcessWire, PDF'); // SetKeywords is internally called on the mPDF instanceMarkus Thomas
Pages2Pdf originally developed by Stefan Wanzenried (wanze).