xmarcos/php-weasyprint

Backport of pontedilana/php-weasyprint for legacy applications running PHP 5.6.

v0.1.0 2022-08-16 23:45 UTC

This package is auto-updated.

Last update: 2024-12-13 22:12:38 UTC


README

CI Status Latest Stable Version Total Downloads License PHP Version Require

PhpWeasyPrint is a wrapper for WeasyPrint, an alternative to wkhtmltopdf.

⚠️ This is a backport of pontedilana/php-weasyprint for legacy applications running PHP 5.6 —which have reached end-of-life and should not be used in production.

This original library is massively inspired by KnpLabs/snappy, of which it aims to be a one-to-one substitute (GeneratorInterface is the same). Checkout its README for more information.

Installation

composer require xmarcos/php-weasyprint

You need to have WeasyPrint installed and available in your path as well.

Usage

Initialization

<?php

require __DIR__ . '/vendor/autoload.php';

use xmarcos\PhpWeasyPrint\Pdf;

$pdf = new Pdf('/usr/bin/weasyprint');

Display the pdf in the browser

$pdf = new Pdf('/usr/bin/weasyprint');
header('Content-Type: application/pdf');
echo $pdf->getOutput('http://www.github.com');

Download the pdf from the browser

$pdf = new Pdf('/usr/bin/weasyprint');
header('Content-Type: application/pdf');
header('Content-Disposition: attachment; filename="file.pdf"');
echo $pdf->getOutput('http://www.github.com');

Generate local pdf file from HTML

$pdf = new Pdf('/usr/bin/weasyprint');
$pdf->generateFromHtml('<h1>Bill</h1><p>You owe me money, dude.</p>', '/tmp/bill-123.pdf');

Generate local pdf file from URL

$pdf = new Pdf('/usr/bin/weasyprint');
$pdf->generate('http://www.github.com', '/local/path-to.pdf');

Pass arguments to weasyprint

See weasyprint Command-line API for an explanation of these options.

// Type weasyprint -h to see the list of options
$pdf = new Pdf('/usr/bin/weasyprint');
$pdf->setOption('encoding', 'utf8');
$pdf->setOption('media-type', 'screen');
$pdf->setOption('presentational-hints');
$pdf->setOption('optimize-size', 'all');
$pdf->setOption('stylesheet', ['/path/to/first-style.css', '/path/to/second-style.css']);
$pdf->setOption('attachment', ['/path/to/image.png', '/path/to/logo.jpg']);

Reset arguments

Options/arguments can be reset to their initial values with resetOptions() method.

$pdf = new Pdf('/usr/bin/weasyprint');
// Set some options
$pdf->setOption('media-type', 'screen');
// ..
// Reset options
$pdf->resetOptions();

Integration Test

There is a very simple integration test included for convenience. It's using the same docker image and exact same python and weasyprint versions that the project that originated this fork requires. Don't expect that to be a very comprehensive suite, just there to ensure it works. That said, run it like this:

# build image
docker build -t php56 .

# run the image, which runs the unit and integration tests
docker run -it --rm php56

#  Expected Output:

# Time: 102 ms, Memory: 5.50MB
# OK (41 tests, 64 assertions)
# Running Integration Test with 'WeasyPrint version 52.5'
#     PDF Generated: OK
#     PDF Size: 17251
#     PDF Content: OK

Bugs & Support

If you need support for a version of PHP other than 5.6, please see the original library. The only goal of this fork is to support legacy applications running PHP 5.6 until they can be upgraded or sunset. It should be possible to use this code for even older versions, but I don't have the need nor the time to try them.

Credits

  • PhpWeasyPrint has been originally developed by the Pontedilana dev team.
  • Snappy has been originally developed by the KnpLabs team.