承接 pontedilana/weasyprint-bundle 相关项目开发

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

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

pontedilana/weasyprint-bundle

Composer 安装命令:

composer require pontedilana/weasyprint-bundle

包简介

Easily create PDF in Symfony by converting Twig/HTML templates.

README 文档

README

PhpWeasyPrint is a PHP (7.4+) wrapper for WeasyPrint PDF generator. It allows you to generate PDF files from HTML string or URL.

The WeasyPrintBundle provides a simple integration for your Symfony project.

This bundle is massively inspired by KnpLabs/KnpSnappyBundle, of which it aims to be a one-to-one substitute

Installation

With composer, require:

composer require pontedilana/weasyprint-bundle

Then enable it in your kernel (a flex recipe is coming soon):

// config/bundles.php
<?php

return [
    //...
    Pontedilana\WeasyprintBundle\WeasyprintBundle::class => ['all' => true],
    //...
];

Configuration

If you need to change the binaries, change the instance options or even disable one or both services, you can do it through the configuration.

# config/packages/weasyprint.yaml
weasyprint:
    pdf:
        enabled:    true
        binary:     /usr/local/bin/weasyprint
        options:    []

Note: prefer an absolute path for binary (e.g. /usr/local/bin/weasyprint). Since php-weasyprint 2.5.1 the binary is verified with is_executable() before being run, and a bare command name like weasyprint is not resolved against the PATH. If you do leave a bare name, the bundle resolves it through the PATH for you (via Symfony's ExecutableFinder), but an absolute path is faster and unambiguous.

If you want to change temporary folder which is sys_get_temp_dir() by default, you can use

# config/packages/weasyprint.yaml
weasyprint:
    temporary_folder: "%kernel.cache_dir%/weasyprint"

You can also configure the timeout used by the generators with process_timeout:

# config/packages/weasyprint.yaml
weasyprint:
    process_timeout: 20 # In seconds

Set it to false to disable the timeout entirely (useful when WeasyPrint runs inside a worker or queue that already manages timeouts):

# config/packages/weasyprint.yaml
weasyprint:
    process_timeout: false

To restrict the URL schemes allowed in options that accept URLs (a defense against SSRF and local file disclosure), use allowed_schemes. When omitted, php-weasyprint applies its own default (['http', 'https']). Requires php-weasyprint 2.6+.

# config/packages/weasyprint.yaml
weasyprint:
    pdf:
        allowed_schemes: ['http', 'https', 'file']

Usage

The bundle registers one service:

  • the weasyprint.pdf service allows you to generate pdf files.

Generate a PDF document from a URL

// @var Pontedilana\PhpWeasyPrint\Pdf
$weasyprintPdf->generate('https://www.github.com', '/path/to/the/file.pdf');

Generate a PDF document from a twig view

// @var Pontedilana\PhpWeasyPrint\Pdf
$weasyprintPdf->generateFromHtml(
    $this->renderView(
        'frontend/product/pdf.html.twig',
        [
            'some'  => $vars,
        ]
    ),
    '/path/to/the/file.pdf'
);

Render a PDF document as a response from a controller

use Pontedilana\WeasyprintBundle\WeasyPrint\Response\PdfResponse;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;

class SomeController extends AbstractController
{
    public function pdf(Pontedilana\PhpWeasyPrint\Pdf $weasyprintPdf)
    {
        $html = $this->renderView(
            'frontend/product/pdf.html.twig',
            [
                'some'  => $vars,
            ]
        );

        return new PdfResponse(
            $weasyprintPdf->getOutputFromHtml($html),
            'file.pdf'
        );
    }
}

Note: Filenames with accented characters (e.g., invoice_àèìòù.pdf) are automatically supported. The bundle generates an ASCII-safe fallback for older browsers while preserving the original UTF-8 filename for modern browsers (following RFC 6266).

Render a PDF document with a relative url inside like CSS files or images

use Pontedilana\WeasyprintBundle\WeasyPrint\Response\PdfResponse;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;

class SomeController extends AbstractController
{
    public function pdf(Pontedilana\PhpWeasyPrint\Pdf $weasyprintPdf)
    {
        $pageUrl = $this->generateUrl('homepage', [], true); // use absolute path!

        return new PdfResponse(
            $weasyprintPdf->getOutput($pageUrl),
            'file.pdf'
        );
    }
}

Credits

WeasyPrintBundle and PhpWeasyPrint have been developed by Pontedilana.
SnappyBundle has been developed by KnpLabs.

pontedilana/weasyprint-bundle 适用场景与选型建议

pontedilana/weasyprint-bundle 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 852.62k 次下载、GitHub Stars 达 40, 最近一次更新时间为 2021 年 07 月 17 日, 在 PHP 生态内属于活跃度较高的组件。

它主要适用于以下技术方向: 「bundle」 「pdf」 「symfony-bundle」 「WeasyPrint」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。

我们在过去多个企业项目中使用过 pontedilana/weasyprint-bundle 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 pontedilana/weasyprint-bundle 我们能提供哪些服务?
定制开发 / 二次开发

基于 pontedilana/weasyprint-bundle 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

  • 总下载量: 852.62k
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 40
  • 点击次数: 19
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 40
  • Watchers: 3
  • Forks: 7
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2021-07-17