Skip to content

markusthomas/Wire2PDF

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

1 Commit
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Wire2PDF

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:

  1. Automatic PDF generation from Pages via URL hooks (formerly Pages2Pdf).
  2. API usage to generate PDFs from HTML strings or files programmatically (formerly WirePDF).

Requirements

  • ProcessWire >= 3.0.0
  • PHP >= 8.0

Installation

  1. Copy the module files to /site/modules/Wire2PDF/.
  2. Login to the ProcessWire admin and install the module Wire2PDF.

Migration Guide (from Pages2Pdf / WirePDF)

If you are migrating from the separate modules Pages2Pdf and WirePDF, follow these steps:

  1. Uninstall the old modules Pages2Pdf and WirePDF.
  2. Install Wire2PDF.
  3. 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.
  4. URL Parameters:
    • The default GET variable has changed from pages2pdf to pdf.
    • 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.
  5. 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.

Configuration

The module configuration combines settings from both predecessors:

Automatic Generation

  • 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.

PDF Engine (mPDF)

  • 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.

Usage: Automatic Generation (via URL)

This feature allows you to download a PDF of any page simply by appending a URL parameter.

  1. Enable the template of the page in the module settings.
  2. 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)
  3. 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.

API Usage

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.

Configuration

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

Metadata & Variables

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.

Custom Fonts

You can upload your own font files to be used in PDF generation.

Uploading Fonts

  1. Go to Modules > Configure > Wire2PDF.
  2. Find the fieldset PDF Engine Settings (WirePDF/mPDF) and expand it.
  3. Inside, find the Font Settings section.
  4. Use the file input to select one or more .ttf font files from your computer.
  5. Click Save at the bottom of the page. The page will reload, and your fonts will be uploaded.
  6. The "Installed Custom Fonts" list will now show your uploaded font files.

Deleting Fonts

To remove a custom font:

  1. In the "Installed Custom Fonts" list, check the trash icon next to the font you want to remove.
  2. Click Save.

Using Custom Fonts

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.

Using the module

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 instance

Author

Markus Thomas

Credits

Pages2Pdf originally developed by Stefan Wanzenried (wanze).

About

Wire2PDF is the successor to and combination of the modules Pages2Pdf and WirePDF.

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors