Jump to content

Recommended Posts

Posted

Hi all,

I'd like to introduce LePdf, a small ProcessWire module that wraps mPDF: you give it HTML, it gives you a PDF. You can show it in the browser, send it as a download, save it to disk, or get it as a string. The mPDF instance stays directly accessible, so nothing is hidden from you.

Why another PDF module?
I use RockPdf in a few projects, and its development has been discontinued. I wanted something small that I can maintain myself, runs on plain shared hosting, and covers what I actually need: output, download, save. LePdf is an independent implementation. The basic idea (a thin wrapper around mPDF) is inspired by RockPdf. I contacted @bernhard beforehand, and he replied that he has no problem with it and is happy about it. ❤️

LePdf is not a drop-in replacement. There is no backwards compatibility with the RockPdf API.

Features

  • write(), css(), renderTemplate(), show(), download(), save(), output()
  • Header, footer, title, author (set globally in the module config or per document)
  • Custom fonts and icon fonts (e.g. FontAwesome)
  • Direct access to the mPDF instance via $pdf->mpdf
  • mPDF is bundled, so no Composer is needed (useful on shared hosting)
  • Module configuration page with an explanation and example for every field
  • Config page translatable with ProcessWire's language system; CSV files for DE, FR, ES, PT, IT, NL are included


Quick start

$pdf = $modules->get('LePdf');          // every get() returns a new, empty PDF
$pdf->write('<h1>Hello</h1><p>This is a PDF.</p>');
$pdf->show('hello.pdf');                // show in the browser and end the script


Example: attendance list with a signature column

<?php namespace ProcessWire;

// check access before anything is generated
if(!$user->isLoggedin() || !$user->hasRole('editor')) throw new Wire404Exception();

$event = $pages->get((int) $input->get->id);

$rows = '';
foreach($event->participants->sort('name') as $u) {
	$rows .= '<tr><td>' . $sanitizer->entities($u->name) . '</td><td></td><td></td></tr>';
}

$pdf = $modules->get('LePdf');
$pdf->title("Attendance {$event->title}");
$pdf->css('td, th { border: 1px solid #000; padding: 4mm; } th { background: #eee; }');
$pdf->write('<h2>' . $sanitizer->entities($event->title) . "</h2>
<table width='100%' style='border-collapse: collapse'>
<tr><th>Name</th><th width='15%'>Present</th><th width='35%'>Signature</th></tr>
$rows
</table>");
$pdf->show('attendance.pdf');

 

The admin configuration page

LePdf_config.thumb.png.bf4d8778fa35cf54af0c9b8305a04148.png

 

Requirements

  • PHP 7.4 or newer (bundled mPDF 8.3 supports up to PHP 8.5)
  • ProcessWire 3.0 or newer
  • PHP extensions mbstring and gd; zlib recommended

Installation
Copy the folder to site/modules/LePdf/, then Modules > Refresh > install LePdf. Details are in the README.

Status
Version 0.1.0, early. I tested it manually on PHP 8.5 on shared hosting; the automated tests (which run without ProcessWire) run on PHP 8.3. Real-world testing on other setups is exactly what I'm missing.

Notes

  • LePdf does not check permissions and does not escape HTML. Access control and escaping belong in your template (see the README).
  • The translations were created with AI assistance and have not been reviewed by native speakers. Corrections are welcome.

 

Feedback
I'd be glad about bug reports, test results on other hosting setups, corrections to the translations, and ideas for what should come next. I'm deliberately keeping version 1 small.

 

License
LePdf is MIT. The bundled mPDF is GPL-2.0-only (see lib/THIRD-PARTY.md in the repo).

GitHub repository

  • Like 14
  • 4 weeks later...
Posted

LePdf 0.1.1 released: QR codes now work

A small update for LePdf (https://github.com/LeRalf/LePdf).

What was wrong: in 0.1.0, QR codes via mPDF's <barcode type="QR"> failed with "Mpdf\QrCode package was not found". mPDF needs the separate package mpdf/qrcode for this, and LePdf did not ship it. Since LePdf bundles all its libraries in lib/ (so no Composer is needed on shared hosting), this was a gap in the bundle.

What changed: mpdf/qrcode 1.2.2 is now bundled in lib/. Nothing in the LePdf API changed, so it is a patch release. The automated tests now include a QR code case.

Usage example:

$pdf->write('<barcode code="2027-TEST-CODE" type="QR" error="M" size="1" disableborder="1" />');


Updating from 0.1.0: replace the whole module folder, including lib/, then refresh modules.

Release notes: https://github.com/LeRalf/LePdf/releases/tag/v0.1.1

Feedback and bug reports are welcome, either here or as GitHub issues.

  • Like 2
  • 4 weeks later...
Posted

LePdf 0.3.0 released (together with 0.2.0): put a signature on an existing PDF, and no-store headers

Two small feature updates since 0.1.1 (repo: https://github.com/LeRalf/LePdf). Nothing in the default behaviour changes, so existing code keeps working.

0.2.0 – existing PDFs as a base:
LePdf can now take a PDF that already exists and put an image or a small HTML box on top of it. My use case is a list that people print, sign and that is then signed digitally on a tablet; the signature from a signature pad is placed on the saved PDF. `open()` takes over all pages unchanged, `stampImage()` places an image, `stampHtml()` places an HTML box. Positions are in mm from the top left corner of the page.

$pdf = $modules->get('LePdf');
$pdf->open("lists/{$person->id}.pdf")        // a PDF saved earlier, for example with save()
    ->stampImage($data, 20, 240, 50)         // $data: "data:image/png;base64,..." from a signature pad; x, y, width, last page
    ->stampHtml('<p style="font-size: 8pt">Signed ' . date('d.m.Y H:i') . '</p>', 20, 262, 100, 8)
    ->save("lists/{$person->id}-signed.pdf");

Things to know:
- No header, footer or margins are added to opened pages, and `write()` and `css()` throw an exception in this mode.
- Images can be PNG, JPEG or GIF (up to 5 MB) as raw data, data URI or local file. Web addresses are not loaded.
- The result contains a picture of the signature. It is not a digital signature in the legal sense (no certificate, no protection against changes).
- Limit: the bundled FPDI importer (the free one that comes with mPDF) cannot read every PDF. PDFs created with LePdf work; PDFs with compressed cross-reference streams throw an exception.

0.3.0 – `noStore()`:
What was wrong: by default `show()` and `download()` send `Cache-Control: private, max-age=0, must-revalidate`. That is fine for most PDFs. In one of my projects a list changes during the day, and some users got an older copy, probably because a viewer or app reused the same address and file name. I have not confirmed the exact cause on the device side.

What changed: `noStore()` sends `Cache-Control: private, no-store, max-age=0`, `Pragma: no-cache` and `Expires: 0` instead. It works for `show()` (inline) and `download()` (attachment) alike. `noStore(false)` returns to the default, which is unchanged.

Usage example:

$pdf->noStore()->show('list.pdf');

If an app still shows an old file, it can help to change the address (for example a time stamp as a parameter) and the file name as well, because some apps reuse files by name.

Updating from 0.1.1:
Replace the module folder, then Modules > Refresh. No new translation strings, no new bundled libraries.

Release notes: https://github.com/LeRalf/LePdf/releases/tag/v0.2.0 and https://github.com/LeRalf/LePdf/releases/tag/v0.3.0

Feedback is welcome, here in the thread or as an issue on GitHub.

  • Like 2

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...