spiritix / html-to-pdf
使用著名的 wkhtmltopdf 库将 HTML 标记转换为漂亮的 PDF 文件
v2.0.2
2016-09-20 13:40 UTC
Requires
- php: >=5.5.9
- h4cc/wkhtmltopdf-amd64: 0.12.3.*
- h4cc/wkhtmltopdf-i386: 0.12.3.*
Requires (Dev)
- codeclimate/php-test-reporter: dev-master
- phpunit/phpunit: ~5.0
This package is auto-updated.
Last update: 2024-09-09 01:11:44 UTC
README
使用著名的 wkhtmltopdf 库将 HTML 标记转换为漂亮的 PDF 文件。
遗憾的是,wkhtmltopdf 基于一个非常过时的 WebKit 版本,并且不再真正维护。因此,我最近推出了一个新版本的库,它基于 Google Chrome。试一试吧!
特性
- 无需复杂的安装或配置,即开即用
- 提供 wkhtmltopdf 的所有功能
- 可以从外部 URL 获取 HTML
- 可以下载、嵌入或保存生成的 PDF 文件(甚至可以将其内容作为字符串获取)
- 支持 PHP7
要求
- PHP 5.5+
- 启用程序执行功能(proc_open)
- 启用 'fopen' 包装器(以便使用 URL 到 PDF 功能)
- 基于 Unix 的操作系统(未来可能会添加 Windows 支持)
安装
HtmlToPdf 可以通过在项目的 composer.json
中添加 spiritix/html-to-pdf
包来使用 Composer 安装。
php composer.phar require spiritix/html-to-pdf
用法
此库的使用非常简单。您只需要一个转换实例,将其传递给一个输入和一个输出处理程序,并根据需要设置一些选项。运行转换后,转换器将为您提供输出处理程序实例。现在您可以使用其特定功能获取 PDF 文件。
use Spiritix\HtmlToPdf\Converter; use Spiritix\HtmlToPdf\Input\UrlInput; use Spiritix\HtmlToPdf\Output\DownloadOutput; $input = new UrlInput(); $input->setUrl('https://www.google.com'); $converter = new Converter($input, new DownloadOutput()); $converter->setOption('n'); $converter->setOption('d', '300'); $converter->setOptions([ 'no-background', 'margin-bottom' => '100', 'margin-top' => '100', ]); $output = $converter->convert(); $output->download();
输入处理程序
以下输入处理程序可用
- StringInput - 接受 HTML 内容作为字符串
- UrlInput - 从 URL 获取 HTML 内容
输出处理程序
以下输出处理程序可用
- StringOutput - 将 PDF 内容作为字符串获取
- FileOutput - 将 PDF 存储到您的文件系统
- DownloadOutput - 强制浏览器下载 PDF 文件
- EmbedOutput - 强制浏览器嵌入 PDF 文件
选项
General Options
allow <path> Allow the file or files from the specified folder to be loaded (repeatable)
b, book* Set the options one would usually set when printing a book
collate Collate when printing multiple copies
cookie <name> <value> Set an additional cookie (repeatable)
cookie-jar <path> Read and write cookies from and to the supplied cookie jar file
copies <number> Number of copies to print into the pdf file (default 1)
cover* <url> Use html document as cover. It will be inserted before the toc with no headers and footers
custom-header <name> <value> Set an additional HTTP header (repeatable)
debug-javascript Show javascript debugging output
H, default-header* Add a default header, with the name of the page to the left, and the page number to the right, this is short for: header-left='[webpage]' header-right='[page]/[toPage]' top 2cm header-line
disable-external-links* Do no make links to remote web pages
disable-internal-links* Do no make local links
n, disable-javascript Do not allow web pages to run javascript
disable-pdf-compression* Do not use lossless compression on pdf objects
disable-smart-shrinking* Disable the intelligent shrinking strategy used by WebKit that makes the pixel/dpi ratio none constant
disallow-local-file-access Do not allowed conversion of a local file to read in other local files, unless explecitily allowed with allow
d, dpi <dpi> Change the dpi explicitly (this has no effect on X11 based systems)
enable-plugins Enable installed plugins (such as flash
encoding <encoding> Set the default text encoding, for input
forms* Turn HTML form fields into pdf form fields
g, grayscale PDF will be generated in grayscale
ignore-load-errors Ignore pages that claimes to have encountered an error during loading
l, lowquality Generates lower quality pdf/ps. Useful to shrink the result document space
B, margin-bottom <unitreal> Set the page bottom margin (default 10mm)
L, margin-left <unitreal> Set the page left margin (default 10mm)
R, margin-right <unitreal> Set the page right margin (default 10mm)
T, margin-top <unitreal> Set the page top margin (default 10mm)
minimum-font-size <int> Minimum font size (default 5)
no-background Do not print background
O, orientation <orientation> Set orientation to Landscape or Portrait
page-height <unitreal> Page height (default unit millimeter)
page-offset* <offset> Set the starting page number (default 1)
s, page-size <size> Set paper size to: A4, Letter, etc.
page-width <unitreal> Page width (default unit millimeter)
password <password> HTTP Authentication password
post <name> <value> Add an additional post field (repeatable)
post-file <name> <path> Post an aditional file (repeatable)
print-media-type* Use print media-type instead of screen
p, proxy <proxy> Use a proxy
q, quiet Be less verbose
redirect-delay <msec> Wait some milliseconds for js-redirects (default 200)
replace* <name> <value> Replace [name] with value in header and footer (repeatable)
stop-slow-scripts Stop slow running javascripts
title <text> The title of the generated pdf file (The title of the first document is used if not specified)
t, toc* Insert a table of content in the beginning of the document
use-xserver* Use the X server (some plugins and other stuff might not work without X11)
user-style-sheet <url> Specify a user style sheet, to load with every page
username <username> HTTP Authentication username
zoom <float> Use this zoom factor (default 1)
Headers And Footer Options
footer-center* <text> Centered footer text
footer-font-name* <name> Set footer font name (default Arial)
footer-font-size* <size> Set footer font size (default 11)
footer-html* <url> Adds a html footer
footer-left* <text> Left aligned footer text
footer-line* Display line above the footer
footer-right* <text> Right aligned footer text
footer-spacing* <real> Spacing between footer and content in mm (default 0)
header-center* <text> Centered header text
header-font-name* <name> Set header font name (default Arial)
header-font-size* <size> Set header font size (default 11)
header-html* <url> Adds a html header
header-left* <text> Left aligned header text
header-line* Display line below the header
header-right* <text> Right aligned header text
header-spacing* <real> Spacing between header and content in mm (default 0)
Table Of Content Options
toc-depth* <level> Set the depth of the toc (default 3)
toc-disable-back-links* Do not link from section header to toc
toc-disable-links* Do not link from toc to sections
toc-font-name* <name> Set the font used for the toc (default Arial)
toc-header-font-name* <name> The font of the toc header (if unset use toc-font-name)
toc-header-font-size* <size> The font size of the toc header (default 15)
toc-header-text* <text> The header text of the toc (default Table Of Contents)
toc-l1-font-size* <size> Set the font size on level 1 of the toc (default 12)
toc-l1-indentation* <num> Set indentation on level 1 of the toc (default 0)
toc-l2-font-size* <size> Set the font size on level 2 of the toc (default 10)
toc-l2-indentation* <num> Set indentation on level 2 of the toc (default 20)
toc-l3-font-size* <size> Set the font size on level 3 of the toc (default 8)
toc-l3-indentation* <num> Set indentation on level 3 of the toc (default 40)
toc-l4-font-size* <size> Set the font size on level 4 of the toc (default 6)
toc-l4-indentation* <num> Set indentation on level 4 of the toc (default 60)
toc-l5-font-size* <size> Set the font size on level 5 of the toc (default 4)
toc-l5-indentation* <num> Set indentation on level 5 of the toc (default 80)
toc-l6-font-size* <size> Set the font size on level 6 of the toc (default 2)
toc-l6-indentation* <num> Set indentation on level 6 of the toc (default 100)
toc-l7-font-size* <size> Set the font size on level 7 of the toc (default 0)
toc-l7-indentation* <num> Set indentation on level 7 of the toc (default 120)
toc-no-dots* Do not use dots, in the toc
Outline Options
dump-outline* <file> Dump the outline to a file
outline* Put an outline into the pdf
outline-depth* <level> Set the depth of the outline (default 4)
Options marked * may not work on some servers.
已知问题和限制
- 在基于 Windows 的系统上无法工作
贡献
欢迎任何形式的贡献。在提交拉取请求之前,请考虑以下指南
- 编码标准 - 主要遵循 PSR-2,有一些差异。
- 添加测试! - 如果您的 PR 没有测试,它将不会被接受。
- 创建功能分支 - 我不会从您的 master 分支拉取。
许可证
HtmlToPdf 是免费软件,根据 MIT 许可证的条款分发。