7个版本 (3个稳定)
| 1.0.2 | 2023年2月15日 |
|---|---|
| 1.0.1 | 2023年2月5日 |
| 1.0.0 | 2022年9月15日 |
| 0.2.0 | 2022年9月15日 |
| 0.1.5 | 2022年9月7日 |
#837 in 文本处理
每月50次下载
用于 label-converter
48KB
649 行
wkhtmlapp
到wkhtmltopdf和wkhtmltoimage的绑定
wkhtmlapp依赖于wkhtmltopdf应用程序来生成PDF或图像,以这种方式抽象化,以支持多线程而不损害主实例。这个库是受barryvdh的laravel-snappy启发的。
| 资源 | 链接 |
|---|---|
| crate | |
| 文档 | Cargo文档 |
| 上游 | wkhtmltopdf.org |
| Wkhtmltox版本 | wkhtmltox-0.12.6-1 |
使用前的必要设置
- 将wkhtmltopdf设置在系统环境变量中的路径或为您的操作系统下载wkhtmltopdf的可移植版本,存储它们,并在.env文件中使用WKHTMLTOPDF_CMD和WKHTMLTOIMG_CMD分别引用。
//.env
WKHTMLAPP_WORK_DIR="storage/temp"
WKHTMLTOPDF_CMD="assets/bin/wkhtmltopdf/0.16/wkhtmltopdf"
WKHTMLTOIMG_CMD="assets/bin/wkhtmltopdf/0.16/wkhtmltoimage"
只有一些参数已被验证,请谨慎使用
功能
- 将HTML代码转换为PDF和IMG
- 将HTML文件转换为PDF和IMG
- 将URL链接转换为PDF和IMG
变更日志
1.0.0
- 在Windows和Linux系统上与actix-web成功测试
0.2.0
- 您可以实例化PdfApp或ImgApp,它们分别仅用于生成PDF或图像,或实例化App,它为您提供了对两者的访问
0.1.5
- 参数值不再是可选的
0.1.4
- 实现了生命周期,允许在发送参数时使用str而不是String
0.1.3
- 修复:返回完整文件路径
示例
use wkhtmlapp::WkhtmlError;
fn main() -> Result<(), WkhtmlError> {
let mut app = wkhtmlapp::App::new()?;
let app_report = app.pdf_app
.set_arg("enable-smart-shrinking", "true")?
.set_arg("title", "Torres")?
.set_arg("header-right", "Página [page] de [toPage]")?
.set_arg("margin-top", "18")?;
let report = app_report.run(
wkhtmlapp::WkhtmlInput::Url("https://w3schools.org.cn/graphics/svg_intro.asp"),
"demo",
)?;
println!("report: {:?}", report);
Ok(())
}
PDF示例
let pdf_app = PdfApp::new().expect("Failed to init PDF Application");
let html_code = r#"<html><body><div>DEMO</div></body></html>"#;
let file_path = pdf_app.run(WkhtmlInput::Html(html_code),"demo")?;
let file_path = pdf_app.run(WkhtmlInput::File("examples/index.html"), "demo")?;
let file_path = pdf_app.run(
WkhtmlInput::Url("https://rust-lang.net.cn/en-US/"),
"demo",
)?;
IMG示例
let mut image_app = ImgApp::new().expect("Failed to init image Application");
let args = HashMap::from([("height", "20"), ("width", "20")]);
let res = image_app
.set_format(ImgFormat::Png)?
.set_args(args)?
.run(WkhtmlInput::File("examples/index.html"), "demo")?;
ImgApp参数
| 选项 | 描述 |
|---|---|
| allow | 允许加载指定文件夹中的文件或文件(可重复) |
| bypass-proxy-for | 绕过主机的代理(可重复) |
| cache-dir | Web缓存目录 |
| checkbox-checked-svg | 在渲染勾选复选框时使用此SVG文件 |
| checked-svg | 在渲染未勾选复选框时使用此SVG文件 |
| cookie | 设置一个额外的cookie(可重复) |
| cookie-jar | 从提供的cookie jar文件中读取和写入cookie |
| crop-h | 设置裁剪的高度 |
| crop-w | 设置裁剪的宽度 |
| crop-x | 设置裁剪的x坐标(默认0) |
| crop-y | 设置裁剪的y坐标(默认0) |
| custom-header | 设置一个额外的HTTP头(可重复) |
| custom-header-propagation | 为每个资源请求添加由--custom-header指定的HTTP头。 |
| no-custom-header-propagation | 不要为每个资源请求添加由--custom-header指定的HTTP头。 |
| debug-javascript | 显示javascript调试输出 |
| no-debug-javascript | 不显示javascript调试输出(默认) |
| 编码 | 设置默认文本编码,用于输入 |
| 格式 | 输出格式 |
| 高度 | 设置屏幕高度(默认从页面内容计算得出)(默认0) |
| 图片 | 加载或打印图片(默认) |
| no-images | 不加载或打印图片 |
| disable-javascript | 不允许网页运行javascript |
| enable-javascript | 允许网页运行javascript(默认) |
| javascript-delay | 等待一些毫秒以便javascript完成(默认200) |
| load-error-handling | 指定如何处理加载失败的页面:中止、忽略或跳过(默认中止) |
| load-media-error-handling | 指定如何处理加载失败的媒体文件:中止、忽略或跳过(默认忽略) |
| disable-local-file-access | 不允许将本地文件转换为读取其他本地文件,除非显式允许使用allow |
| enable-local-file-access | 允许将本地文件转换为读取其他本地文件。(默认) |
| minimum-font-size | 最小字体大小 |
| password | HTTP认证密码 |
| disable-plugins | 禁用已安装的插件(默认) |
| enable-plugins | 启用已安装的插件(插件可能无法正常工作) |
| post | 添加额外的post字段 |
| post-file | 发布额外的文件 |
| proxy | 使用代理 |
| quality | 输出图像质量(介于0到100之间)(默认94) |
| quiet | 更简洁 |
| radiobutton-checked-svg | 渲染选定的单选按钮时使用此SVG文件 |
| radiobutton-svg | 渲染未选定的单选按钮时使用此SVG文件 |
| run-script | 页面加载完成后运行此额外的javascript(可重复) |
| disable-smart-width | 使用指定的宽度,即使它不足以容纳内容 |
| enable-smart-width | 扩展--width以适应不可分割的内容(默认) |
| stop-slow-scripts | 停止慢速运行的javascript |
| no-stop-slow-scripts | 不停止慢速运行的javascript(默认) |
| transparent | 在pngs中使背景透明 * |
| use-xserver | 使用X服务器(某些插件和其他内容可能在没有X11的情况下无法工作) |
| user-style-sheet | 指定一个用户样式表,与每个页面一起加载 |
| username | HTTP认证用户名 |
| width | 设置屏幕宽度(默认为1024) |
| window-status | 在渲染页面之前等待window.status等于此字符串 |
| zoom | 使用此缩放因子(默认1) |
PdfApp Args
| 选项 | 描述 |
|---|---|
| collate | 全局选项 |
| no-collate | 全局选项 |
| cookie-jar | 全局选项 |
| copies | 全局选项 |
| dpi | 全局选项 |
| extended-help | 全局选项 |
| grayscale | 全局选项 |
| help | 全局选项 |
| htmldoc | 全局选项 |
| ignore-load-errors | 全局选项 - 旧版v0.9 |
| image-dpi | 全局选项 |
| image-quality | 全局选项 |
| license | 全局选项 |
| log-level | 全局选项 |
| lowquality | 全局选项 |
| manpage | 全局选项 |
| margin-bottom | 全局选项 |
| margin-left | 全局选项 |
| margin-right | 全局选项 |
| margin-top | 全局选项 |
| orientation | 全局选项 |
| page-height | 全局选项 |
| page-size | 全局选项 |
| page-width | 全局选项 |
| no-pdf-compression | 全局选项 |
| quiet | 全局选项 |
| read-args-from-stdin | 全局选项 |
| readme | 全局选项 |
| title | 全局选项 |
| use-xserver | 全局选项 |
| version | 全局选项 |
| dump-default-toc-xsl | 大纲选项 |
| dump-outline | 大纲选项 |
| outline | 大纲选项 |
| no-outline | 大纲选项 |
| outline-depth | 大纲选项 |
| output-format | 大纲选项 |
| allow | 页面选项 |
| background | 页面选项 |
| no-background | 页面选项 |
| bypass-proxy-for | 页面选项 |
| cache-dir | 页面选项 |
| checkbox-checked-svg | 页面选项 |
| checkbox-svg | 页面选项 |
| cookie | 页面选项 |
| custom-header | 页面选项 |
| custom-header-propagation | 页面选项 |
| no-custom-header-propagation | 页面选项 |
| debug-javascript | 页面选项 |
| no-debug-javascript | 页面选项 |
| default-header | 页面选项 |
| 编码 | 页面选项 |
| disable-external-links | 页面选项 |
| enable-external-links | 页面选项 |
| disable-forms | 页面选项 |
| enable-forms | 页面选项 |
| 图片 | 页面选项 |
| no-images | 页面选项 |
| disable-internal-links | 页面选项 |
| enable-internal-links | 页面选项 |
| disable-javascript | 页面选项 |
| enable-javascript | 页面选项 |
| javascript-delay | 页面选项 |
| keep-relative-links | 页面选项 |
| load-error-handling | 页面选项 |
| load-media-error-handling | 页面选项 |
| disable-local-file-access | 页面选项 |
| enable-local-file-access | 页面选项 |
| minimum-font-size | 页面选项 |
| exclude-from-outline | 页面选项 |
| include-in-outline | 页面选项 |
| page-offset | 页面选项 |
| password | 页面选项 |
| disable-plugins | 页面选项 |
| enable-plugins | 页面选项 |
| post | 页面选项 |
| post-file | 页面选项 |
| print-media-type | 页面选项 |
| no-print-media-type | 页面选项 |
| proxy | 页面选项 |
| proxy-hostname-lookup | 页面选项 |
| radiobutton-checked-svg | 页面选项 |
| radiobutton-svg | 页面选项 |
| redirect-delay | 页面选项 // 旧版v0.9 |
| resolve-relative-links | 页面选项 |
| run-script | 页面选项 |
| 禁用智能缩放 | 页面选项 |
| 启用智能缩放 | 页面选项 |
| ssl-crt-path | 页面选项 |
| ssl-key-password | 页面选项 |
| ssl-key-path | 页面选项 |
| stop-slow-scripts | 页面选项 |
| no-stop-slow-scripts | 页面选项 |
| 禁用目录后链 | 页面选项 |
| 启用目录后链 | 页面选项 |
| user-style-sheet | 页面选项 |
| username | 页面选项 |
| 视口大小 | 页面选项 |
| window-status | 页面选项 |
| zoom | 页面选项 |
| 页脚居中 | 页眉和页脚选项 |
| 页脚字体名称 | 页眉和页脚选项 |
| 页脚字体大小 | 页眉和页脚选项 |
| 页脚HTML | 页眉和页脚选项 |
| 页脚左对齐 | 页眉和页脚选项 |
| 页脚线条 | 页眉和页脚选项 |
| 无页脚线条 | 页眉和页脚选项 |
| 页脚右对齐 | 页眉和页脚选项 |
| 页脚间距 | 页眉和页脚选项 |
| 页眉居中 | 页眉和页脚选项 |
| 页眉字体名称 | 页眉和页脚选项 |
| 页眉字体大小 | 页眉和页脚选项 |
| 页眉HTML | 页眉和页脚选项 |
| 页眉左对齐 | 页眉和页脚选项 |
| 页眉线条 | 页眉和页脚选项 |
| 无页眉线条 | 页眉和页脚选项 |
| 页眉右对齐 | 页眉和页脚选项 |
| 页眉间距 | 页眉和页脚选项 |
| 替换 | 页眉和页脚选项 |
| 封面 | 封面对象 |
| 目录 | 目录对象 |
| 禁用点划线 | 目录选项 |
| 目录深度 | 目录选项 - 旧版 v0.9 |
| 目录字体名称 | 目录选项 - 旧版 v0.9 |
| 目录一级字体大小 | 目录选项 - 旧版 v0.9 |
| 目录标题文本 | 目录选项 |
| 目录标题字体名称 | 目录选项 - 旧版 v0.9 |
| 目录标题字体大小 | 目录选项 - 旧版 v0.9 |
| 目录级别缩进 | 目录选项 |
| 禁用目录链接 | 目录选项 |
| 目录文本大小缩小 | 目录选项 |
| xsl样式表 | 目录选项 |
许可证
MIT
自由软件,太棒了!
依赖项
约400–700KB
约14K SLoC