PHP library allowing PDF generation or snapshot from an URL or an HTML page. Wrapper for Kozea/WeasyPrint

Related tags

PDF php pdf weasyprint
Overview

PhpWeasyPrint

PhpWeasyPrint is a PHP library allowing PDF generation from an URL or an HTML page. It's a wrapper for WeasyPrint, a smart solution helping web developers to create PDF documents, available everywhere Python runs.

You will have to download and install WeasyPrint to use PhpWeasyPrint (version 53 or greater is recommended).

This library is massively inspired by KnpLabs/snappy, of which it aims to be a one-to-one substitute (GeneratorInterface is the same). See "Differences with Snappy" section to see how the two differs

Installation using Composer

$ composer require pontedilana/php-weasyprint

Usage

Initialization

<?php

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

use Pontedilana\PhpWeasyPrint\Pdf;

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

// or you can do it in two steps
$pdf = new Pdf();
$pdf->setBinary('/usr/local/bin/weasyprint');

Display the pdf in the browser

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

Download the pdf from the browser

$pdf = new Pdf('/usr/local/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

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

Pass options to PhpWeasyPrint

// Type weasyprint -h to see the list of options
$pdf = new Pdf('/usr/local/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 options

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

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

Differences with Snappy

Although PhpWeasyPrint and Snappy are interchangeable, there are a couple of differences between the two, due to WeasyPrint cli API:

  • WeasyPrint doesn't support multiple sources to be merged in one single output pdf, so only one input source (string or URL) is accepted in PhpWeasyPrint;
  • WeasyPrint version >= 53 doesn't generate images, so image generation from HTML tring or URL is possible only with WeasyPrint lower versions (Pontedilana\PhpWeasyPrint\Image has been successfully tested with Weasyprint 52.5).

Bugs & Support

If you found a bug please fill a detailed issue with all the following points. If you need some help, please at least provide a complete reproducer so we could help you based on facts rather than assumptions.

  • OS and its version
  • WeasyPrint, its version and how you installed it
  • A complete reproducer with relevant PHP and html/css/js code

If your reproducer is big, please try to shrink it. It will help everyone to narrow the bug.

Credits

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

You might also like...
Convert HTML to PDF using Webkit (QtWebKit)

wkhtmltopdf and wkhtmltoimage wkhtmltopdf and wkhtmltoimage are command line tools to render HTML into PDF and various image formats using the QT Webk

plugin de criação de PDF através do HTML fácil

pluginmpdf plugin de criação de PDF através do HTML Para inciar nosso pluginmpdf devemos instalar a lib abaixo. mPDF is a PHP library which generates

Official clone of PHP library to generate PDF documents and barcodes

TCPDF PHP PDF Library Please consider supporting this project by making a donation via PayPal category Library author Nicola Asuni [email protected] co

TCPDF - PHP PDF Library - https://tcpdf.org

tc-lib-pdf PHP PDF Library UNDER DEVELOPMENT (NOT READY) UPDATE: CURRENTLY ALL THE DEPENDENCY LIBRARIES ARE ALMOST COMPLETE BUT THE CORE LIBRARY STILL

Pdf and graphic files generator library written in php

Information Examples Sample documents are in the "examples" directory. "index.php" file is the web interface to browse examples, "cli.php" is a consol

PdfParser, a standalone PHP library, provides various tools to extract data from a PDF file.

PdfParser Pdf Parser, a standalone PHP library, provides various tools to extract data from a PDF file. Website : https://www.pdfparser.org Test the A

Official clone of PHP library to generate PDF documents and barcodes

TCPDF PHP PDF Library Please consider supporting this project by making a donation via PayPal category Library author Nicola Asuni [email protected] co

A PHP tool that helps you write eBooks in markdown and convert to PDF.
A PHP tool that helps you write eBooks in markdown and convert to PDF.

Artwork by Eric L. Barnes and Caneco from Laravel News ❤️ . This PHP tool helps you write eBooks in markdown. Run ibis build and an eBook will be gene

Generate simple PDF invoices with PHP
Generate simple PDF invoices with PHP

InvoiScript Generate simple PDF invoices with PHP. Installation Run: composer require mzur/invoiscript Usage Example use Mzur\InvoiScript\Invoice; re

Comments
  • weasyprint: error: unrecognized arguments: --format

    weasyprint: error: unrecognized arguments: --format

    The exit status code '2' says something went wrong: stderr: "usage: weasyprint [-h] [--version] [-i] [-e ENCODING] [-s STYLESHEET] [-m MEDIA_TYPE] [-u BASE_URL] [-a ATTACHMENT] [-p] [-O {images,fonts,all,none}] [-v] [-d] [-q] input output weasyprint: error: unrecognized arguments: --format C:\Users\AEM\AppData\Local\Temp\php_weasyprint61cbe3514c1101.17556704.pdf " stdout: "" command: C:\Users\AEM\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.8_qbz5n2kfra8p0\LocalCache\local-packages\Python38\Scripts\weasyprint.exe --format pdf "http://www.github.com" "C:\Users\AEM\AppData\Local\Temp\php_weasyprint61cbe3514c1101.17556704.pdf".

    if i run in command line , i get same error

    when i run this command is ok weasyprint https://example.com weasyprint-website.pdf

    opened by khonyuen 3
  • Configure Renovate

    Configure Renovate

    WhiteSource Renovate

    Welcome to Renovate! This is an onboarding PR to help you understand and configure settings before regular Pull Requests begin.

    🚦 To activate Renovate, merge this Pull Request. To disable Renovate, simply close this Pull Request unmerged.


    Detected Package Files

    • composer.json (composer)
    • .github/workflows/ci.yaml (github-actions)

    Configuration Summary

    Based on the default config's presets, Renovate will:

    • Start dependency updates only once this onboarding PR is merged
    • Enable Renovate Dependency Dashboard creation
    • If semantic commits detected, use semantic commit type fix for dependencies and chore for all others
    • Ignore node_modules, bower_components, vendor and various test/tests directories
    • Autodetect whether to pin dependencies or maintain ranges
    • Rate limit PR creation to a maximum of two per hour
    • Limit to maximum 20 open PRs at any time
    • Group known monorepo packages together
    • Use curated list of recommended non-monorepo package groupings
    • Fix some problems with very old Maven commons versions
    • Ignore spring cloud 1.x releases
    • Ignore http4s digest-based 1.x milestones
    • Use node versioning for @types/node
    • Limit concurrent requests to reduce load on Repology servers until we can fix this properly, see issue 10133

    🔡 Would you like to change the way Renovate is upgrading your dependencies? Simply edit the renovate.json in this branch with your custom config and the list of Pull Requests in the "What to Expect" section below will be updated the next time Renovate runs.


    What to Expect

    With your current configuration, Renovate will create 4 Pull Requests:

    Pin dependencies
    Update dependency phpstan/phpstan to v1
    • Schedule: ["at any time"]
    • Branch name: renovate/phpstan-phpstan-1.x
    • Merge into: main
    • Upgrade phpstan/phpstan to 1.2.0
    Update dependency phpstan/phpstan-phpunit to v1
    • Schedule: ["at any time"]
    • Branch name: renovate/phpstan-phpstan-phpunit-1.x
    • Merge into: main
    • Upgrade phpstan/phpstan-phpunit to 1.0.0
    Update dependency psr/log to v3
    • Schedule: ["at any time"]
    • Branch name: renovate/psr-log-3.x
    • Merge into: main
    • Upgrade psr/log to ^3.0

    🚸 Branch creation will be limited to maximum 2 per hour, so it doesn't swamp any CI resources or spam the project. See docs for prhourlylimit for details.


    ❓ Got questions? Check out Renovate's Docs, particularly the Getting Started section. If you need any further assistance then you can also request help here.


    This PR has been generated by WhiteSource Renovate. View repository job log here.

    opened by renovate[bot] 1
  • Stylesheets not able to be an array fix

    Stylesheets not able to be an array fix

    When trying to add stylesheets to the PDF document, I got an error as the isFile method only allowed strings. Adding an is_string check fixed this issue.

    opened by harry3110 0
Releases(0.11.0)
  • 0.11.0(Feb 28, 2022)

    What's Changed

    • Bump symfony/process up to ^6.0 and psr/log up to ^3.0 by @romainf in https://github.com/pontedilana/php-weasyprint/pull/3

    New Contributors

    • @romainf made their first contribution in https://github.com/pontedilana/php-weasyprint/pull/3

    Full Changelog: https://github.com/pontedilana/php-weasyprint/compare/0.10.1...0.11.0

    Source code(tar.gz)
    Source code(zip)
  • 0.10.1(Dec 29, 2021)

  • 0.10.0(Dec 29, 2021)

    Removed --format option default value, this option was deprecated in WeasyPrint 53 and removed in WeasiPrint 54. If you still use WeasyPrint 52 or lower you can set this option back with ->setOption():

    $weasyprint = new \Pontedilana\PhpWeasyPrint\Pdf('/usr/local/bin/weasyprint');
    $weasyprint->setOption('format', 'pdf');
    

    Changed

    • Unset --format option in Pdf class which is deprecated in WeasyPrint 53 and removed in WeasyPrint 54
    Source code(tar.gz)
    Source code(zip)
  • 0.9.0(Dec 29, 2021)

Owner
Pontedilana
Pontedilana
PHP library allowing thumbnail, snapshot or PDF generation from a url or a html page.

Snappy Snappy is a PHP library allowing thumbnail, snapshot or PDF generation from a url or a html page. It uses the excellent webkit-based wkhtmltopd

KNP Labs 4.1k Dec 30, 2022
⚡️ MIRROR — A feature-rich Laravel wrapper for the WeasyPrint Document Factory.

WeasyPrint for Laravel A feature-rich Laravel wrapper for the WeasyPrint Document Factory. This package requires Laravel 8.47+ running on PHP 8+ in or

Mike Rockétt 7 Dec 30, 2022
Gravity PDF is a GPLv2-licensed WordPress plugin that allows you to automatically generate, email and download PDF documents using Gravity Forms.

Gravity PDF Gravity PDF is a GPLv2-licensed WordPress plugin that allows you to automatically generate, email and download PDF documents using the pop

Gravity PDF 90 Nov 14, 2022
Magento 2 Invoice PDF Generator - helps you to customize the pdf templates for Magento 2

Magento 2 Invoice PDF Generator - helps you to customize the pdf templates for Magento 2. If you have an enabled template and a default template for the store you need your template the system will print the pdf template.

EAdesign 64 Oct 18, 2021
PHP library generating PDF files from UTF-8 encoded HTML

mPDF is a PHP library which generates PDF files from UTF-8 encoded HTML. It is based on FPDF and HTML2FPDF (see CREDITS), with a number of enhancement

null 3.8k Jan 2, 2023
HTML to PDF converter for PHP

Dompdf Dompdf is an HTML to PDF converter At its heart, dompdf is (mostly) a CSS 2.1 compliant HTML layout and rendering engine written in PHP. It is

null 9.3k Jan 1, 2023
Simple wrapper package around MPDF's setProtection method that allows you to set password on PDF files

Laravel PDF Protect (fork) Simple wrapper package around MPDF's setProtection method that allows you to set password on PDF files. Installation You ca

Raphael Planer 2 Jan 23, 2022
Convert HTML to PDF using Webkit (QtWebKit)

wkhtmltopdf and wkhtmltoimage wkhtmltopdf and wkhtmltoimage are command line tools to render HTML into PDF and various image formats using the QT Webk

wkhtmltopdf 13k Jan 4, 2023
Convert html to an image, pdf or string

Convert a webpage to an image or pdf using headless Chrome The package can convert a webpage to an image or pdf. The conversion is done behind the sce

Spatie 4.1k Jan 1, 2023
Laravel package to convert HTML to PDF, supporting multiple drivers.

eve/pdf-converter A Laravel package to help convert HTML to PDF. Supports multiple drivers. Requirements and Installation eve/pdf-converter requires L

eve.io 11 Feb 15, 2022