bymayo/pdf-transform
Composer 安装命令:
composer require bymayo/pdf-transform
包简介
Transform a PDF page to an image (JPEG, PNG)
README 文档
README
PDF Transform for Craft CMS 5
PDF Transform is a Craft CMS plugin that transforms a PDF stored in Assets, to an image. This can then be output via Twig in to a template.
A use case for this is to show the preview of a PDF before a user downloads that particular file.
Features
- Transform PDF's to images via Twig (The file needs to be an existing Asset element)
- PDF's are transformed to an image when PDF's are uploaded via Assets or Asset fields.
- Transformed PDF images are indexed and available in Assets like all other asset elements.
- Works with local asset volumes, Amazon S3 and Servd.
- Control where generated images are placed (volume root, subfolder, or mirror source path).
- Restrict auto-transform to specific source volumes.
Install
- Install with Composer via
composer require bymayo/pdf-transformfrom your project directory - Enable / Install the plugin in the Craft Control Panel under
Settings > Plugins - Customise the plugin settings, especially the
Image Volumeoption.
You can also install the plugin via the Plugin Store in the Craft Admin CP by searching for PDF Transform.
Requirements
- Craft CMS 5.x
- Imagick / Ghostscript
- MySQL (No PostgreSQL support)
Configuration
All settings can be configured from the plugin settings page in the Control Panel, or via a config/pdf-transform.php config file.
| Setting | Default | Description |
| Page Number | 1 |
Set which page in the PDF should be converted to an image. |
| Image Volume | null |
Choose which volume converted images should be stored in. |
| Output Destination | root |
Where within the output volume to place generated images. Options: root (volume root), subfolder (static subfolder), mirror (mirror the source PDF's folder path). |
| Subfolder Name | '' |
When Output Destination is set to subfolder, the name of the subfolder to place images in (e.g. thumbnails). |
| Image Resolution | 72 |
Set the resolution of the converted image. |
| Image Quality | 100 |
Set the image quality of the converted image. |
| Clean Filenames | false |
Remove .pdf from output filenames (e.g. document-123.jpg instead of document.pdf-123.jpg). |
| Colour Space | srgb |
Colour space for the converted image. Options: srgb, rgb, cmyk, gray, none. sRGB fixes inverted colours on CMYK PDFs. |
| Image Format | jpg |
Set the file format of the converted image. Options: jpg, png. |
| Source Volumes | ['*'] |
Which volumes trigger auto-transform when a PDF is uploaded. Set to ['*'] for all volumes, or an array of volume IDs. This does not affect the craft.pdfTransform.render() Twig method. |
Config File
You can override plugin settings by creating a config/pdf-transform.php file in your Craft project:
<?php return [ 'page' => 1, 'imageVolume' => null, 'imageDestination' => 'root', 'imageSubfolder' => '', 'imageFormat' => 'jpg', 'imageResolution' => 72, 'imageQuality' => 100, 'cleanFilenames' => false, 'imageColorspace' => 'srgb', 'sourceVolumes' => ['*'], ];
This supports Craft's multi-environment config format.
Templating
To transform a PDF to an image use the following Twig tag:
{% set pdfToTransform = entry.pdfAsset.one() %}
{% set transformedPdf = craft.pdfTransform.render(pdfToTransform) %}
{{ transformedPdf.url }}
The transformed PDF (Now an image stored in Assets) can then be output using {{ transformedPdf.url }}. Or get any Asset property e.g. title, id, filename etc.
If the transformed image doesn't exist then the PDF will be transformed via the template. This may cause the template/page to become slow whilst the PDF is transformed.
Be aware that this also may output a large image, so we'd recommend running this through an image transform. See Dimensions.
Known Issues
Imagick / Ghostscript
The plugin runs PDFs through a PDF library called pdf-to-image. They have known issues with Imagick where transforms may fail if Ghost Script isn't accessible through Imagick (Very easily resolvable)
Read more about this issue - https://github.com/spatie/pdf-to-image#issues-regarding-ghostscript.
Dimensions
PDF Transform does the basic job of converting your PDF to a single image. It will never be it's role to set width and height dimensions (Other than Image Resolution).
I'd recommend running the PDF image through one of the following options/plugins and setting the dimensions that way (Some of these also handle caching the image as well)
Support
If you have any issues (Surely not!) then I'll aim to reply to these as soon as possible. If it's a site-breaking-oh-no-what-has-happened moment, then hit me up on the Craft CMS Discord - @bymayo
bymayo/pdf-transform 适用场景与选型建议
bymayo/pdf-transform 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 11.8k 次下载、GitHub Stars 达 12, 最近一次更新时间为 2018 年 10 月 26 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「cms」 「Craft」 「craftcms」 「craft-plugin」 「pdf transform」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 bymayo/pdf-transform 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 bymayo/pdf-transform 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 bymayo/pdf-transform 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
Expand, collapse, change the status of, or delete multiple blocks in a Matrix field simultaneously.
GraphQL authentication for your headless Craft CMS applications.
Set Links with a specific language parameter
Supercharged text field validation.
Integrate with Snipcart.
Restrict user registration by domain in Craft CMS, allows you to limit who can register for your site based on their email address.
统计信息
- 总下载量: 11.8k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 12
- 点击次数: 19
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2018-10-26