承接 xmarcos/php-weasyprint 相关项目开发

从需求分析到上线部署,全程专人跟进,保证项目质量与交付效率

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

xmarcos/php-weasyprint

最新稳定版本:v0.1.0

Composer 安装命令:

composer require xmarcos/php-weasyprint

包简介

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

README 文档

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.

统计信息

  • 总下载量: 13
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 2
  • 点击次数: 0
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 1
  • Watchers: 0
  • Forks: 13
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2022-08-16