Git Product home page Git Product logo

php-weasyprint's Introduction

PhpWeasyPrint

PhpWeasyPrint is a PHP library allowing PDF generation from a 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 56 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('https://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('https://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', true);
$pdf->setOption('optimize-images', true);
$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 string or URL is possible only with WeasyPrint lower versions and an unsupported PhpWeasyPrint version (Pontedilana\PhpWeasyPrint\Image has been successfully tested with Weasyprint 52.5 on PhpWeasyPrint 0.13.0).

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.

php-weasyprint's People

Contributors

barryvdh avatar endelwar avatar mbabker avatar romainf avatar stephpy avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar

php-weasyprint's Issues

My image isn't appearing

Hello

Initially I was thinking this might be a WeasyPrint issue, but the command line version is working correctly.
Here are a couple of real URLs as examples you can play with.

First a real image that will work:
https://sobs.com.au/docs/pix/simple-example.png

Second, a secure image retrieval that doesn't produce any image (but it does work when using WeasyPrint from the command line):
https://sobs.com.au/ui/fap.php?fap=WyJub3RpY2VBdHRhY2htZW50Iiw3NjI5MjksIi4uXC91cGxvYWRzXC8xNVwvbm90aWNlc1wvMjAyM1wvMDVcLzc2MjkyOV8xNjg0MTQwOTE5XzEuanBnIl0

Cheers
Murray

Weasyprint Process Timeout

Good day

I have a Codeigniter PHP web app that implements php-weasyprint. I sometimes (5% of the time) get the following error when a user generates a document:

The process "/usr/local/bin/weasyprint '/tmp/php_weasyprint6489782e31d4e8.37601559.html' '.../d9cb68c13f6489782e30fcf.pdf'" exceeded the timeout of 10 seconds.

I cannot seem to see why this happens. It is also interesting, because when the error appears, it is almost immediately after the quotation generation has been initiated, not 10 seconds later.

Is there any reason why this could be happening? I am not sure what code to share, but at least this is my document generator function:

`

          class Document_Generator
          {
              function create_pdf(
                  $html,
                  $complete_relative_path
              ) {
          
                  $pdf = new Pdf('/usr/local/bin/weasyprint');
                  if (file_exists($complete_relative_path)) {
                      unlink($complete_relative_path);
                  }
                  return $pdf->generateFromHtml($html, $complete_relative_path);
              }
          }

`

Difference in rendering

First of all, thanks for this awesome package.
I was looking exactly for a way to replace snappy (and wkhtmltopdf) with something still supported.
So, I tried to remove snappy and install this library in a project of mine, and the migration was really smooth.

The problems raised when I tried to render a pdf: the page layout is quite different, with many wrong positions (I guess related to margins, but I'm not sure)

I understand this is not strictly related to this library, but rather to the differences between wkhtmltopdf and wasyprint, however I was wondering if someone experienced the same and could provide a way to ease the migration path. If so, it could be mentioned in the README. Otherwise, this could be mentioned in the list of differences.

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

PHP 8.3.7 -- impossible to set CLI flag via AbstractGenerator::setOption (function requires name/value pair)

What I tried:

$pdf->setOption('presentational-hints');

What I expected: for this to be accepted.
What actually happened:

Too few arguments to function Pontedilana\PhpWeasyPrint\AbstractGenerator::setOption()

What I tried:

$pdf->setOption('presentational-hints', '');

What I expected: for this to be accepted.
What actually happened:

weasyprint: error: unrecognized arguments: /tmp/php_weasyprint664ca461dfe6e2.63654685.pdf

(likely due to the cmdline being --presentational-hints '' instead of --presentational-hints.

Fun fact: you list $pdf->setOption('presentational-hints'); in the Readme, when it doesn't actually seem to work.

What do?

php unit testing

Hello

I'm trying to implement some php unit testing around my PDF generation.
I can easily check the creation of the PDF document, but if I want to ensure it contains the text I'm looking for, formatted in a similar manner, I'm having trouble.

I found this article (https://stackoverflow.com/questions/32489513/software-testing-a-pdf-is-created-correctly) which suggested generating the PDF and comparing it to a previously generated PDF, which I tried, but it didn't work. Even though both contained exactly the same content the strings were never the same.

Do you have any suggestions how I might be able to ensure my PDF contains the correct content?

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.