9个版本

0.3.0 2024年7月22日
0.2.71 2024年5月2日
0.2.70 2024年4月15日
0.1.1 2024年3月20日

文本处理中排名#216

Download history 62/week @ 2024-04-18 21/week @ 2024-04-25 242/week @ 2024-05-02 6/week @ 2024-05-09 21/week @ 2024-05-16 40/week @ 2024-05-23 15/week @ 2024-05-30 21/week @ 2024-06-06 9/week @ 2024-06-13 13/week @ 2024-06-20 15/week @ 2024-06-27 67/week @ 2024-07-04 73/week @ 2024-07-11 166/week @ 2024-07-18 166/week @ 2024-07-25 63/week @ 2024-08-01

每月下载量468
用于ssg

MIT/Apache

230KB
922

PDF Composer

为Rust从Yaml Front Matter文档生成PDF

概述

此crate从YAML Front Matter源文档创建PDF文档。YAML值可用于PDF字典条目,或用于替换YAML Front Matter文档Markdown部分的占位符引用。

功能

PDF输出目标

生成的PDF保存到项目根目录相对的输出目标目录。

例如,my_pdf_instance.set_output_directory("output_pdfs")。如果没有设置输出目标,则PDF将保存在名为pdf_composer_pdfs的目录中。这样可以防止项目根目录中充满生成的PDF文件。整洁又方便。

PDF版本

目前仅支持PDF规范的最新两个版本(并推荐),即版本1.72.0

例如,my_pdf_instance.set_pdf_version(PDFVersion::V2_0)。如果没有设置PDF版本,则默认使用版本1.7

注意:这不要与PDF本身的版本/版别混淆。这是您自己决定的事情(也许可以将它作为YAML值设置,作为PDF字典条目的一部分,下面将提供更多信息)。

要设置版本,请查看显示对应枚举和版本号的表格。

枚举 PDF版本
PDFVersion::V1_7 1.7
PDFVersion::V2_0 2.0

纸张大小

支持的纸张大小列表。例如,my_pdf_instance.set_paper_size(PaperSize::A5)。如果没有设置纸张大小,则默认纸张大小为A4

ISO 216 A系列纸张尺寸

尺寸 宽度 x 高度(毫米) 宽度 x 高度(英寸)
A0 841mm x 1189mm 33.1" x 46.8"
A1 594mm x 841mm 23.4" x 33.1"
A2 420mm x 594mm 16.5" x 23.4"
A3 297mm x 420mm 11.7" x 16.5"
A4 210mm x 297mm 8.3" x 11.7"
A5 148mm x 210mm 5.8" x 8.3"
A6 105mm x 148mm 4.1" x 5.8"
A7 74mm x 105mm 2.9" x 4.1"
A8 52mm x 74mm 2.0" x 2.9"
A9 37mm x 52mm 1.5" x 2.0"
A10 26mm x 37mm 1.0" x 1.5"

ISO 216 B系列纸张尺寸

尺寸 宽度 x 高度(毫米) 宽度 x 高度(英寸)
B0 1000mm x 1414mm 39.4" x 55.7"
B1 707mm x 1000mm 27.8" x 39.4"
B2 500mm x 707mm 19.7" x 27.8"
B3 353mm x 500mm 13.9" x 19.7"
B4 250mm x 353mm 9.8" x 13.9"
B5 176mm x 250mm 6.9" x 9.8"
B6 125mm x 176mm 4.9" x 6.9"
B7 88mm x 125mm 3.5" x 4.9"
B8 62mm x 88mm 2.4" x 3.5"
B9 44mm x 62mm 1.7" x 2.4"
B10 31mm x 44mm 1.2" x 1.7"

美国纸张尺寸

尺寸 宽度 x 高度(毫米) 宽度 x 高度(英寸)
半信函 140mm x 216mm 5.5" x 8.5"
信函 216mm x 279mm 8.5" x 11"
法律纸 216mm x 356mm 8.5" x 14"
青年法律纸 203mm x 127mm 8" x 5"
对账单 432mm x 279mm 17" x 11"
小报 279mm x 432mm 11" x 17"

JIS纸张尺寸

尺寸 宽度 x 高度(毫米) 宽度 x 高度(英寸)
B0 1030 x 1456 mm 40.6 x 57.3 in
B1 728 x 1030 mm 28.7 x 40.6 in
B2 515 x 728 mm 20.3 x 28.7 in
B3 364 x 515 mm 14.3 x 20.3 in
B4 257 x 364 mm 10.1 x 14.3 in
B5 182 x 257 mm 7.2 x 10.1 in
B6 128 x 182 mm 5.0 x 7.2 in
B7 91 x 128 mm 3.6 x 5.0 in
B8 64 x 91 mm 2.5 x 3.6 in
B9 45 x 64 mm 1.8 x 2.5 in
B10 32 x 45 mm 1.3 x 1.8 in

纸张方向

这允许将PDF文档页面保存为横向或纵向方向。例如,my_pdf_instance.set_orientation(PaperOrientation::Landscape)。如果没有设置方向,默认为Portrait

页面边距

边距使用的单位是毫米(mm)。如果没有设置边距,默认边距大小为10

使用CSS缩写符号设置页面边距(上 右 下 左)。这意味着

设置一个值,然后四个边距都将获得该值。例如,my_pdf_instance.set_margins("20")将导致以下结果

  • margin-top: 20,
  • margin-right: 20,
  • margin-bottom: 20,
  • margin-left: 20

设置两个值,则上边和下边边距将获得第一个值,右边和左边边距将获得第二个值。例如,my_pdf_instance.set_margins("20 15")将导致以下结果

  • margin-top: 20,
  • margin-right: 15,
  • margin-bottom: 20,
  • 左边距:15

设置三个值后,顶部边距将获得第一个值,左右边距将获得第二个值,底部边距将获得第三个值。例如,my_pdf_instance.set_margins("20 15 30") 将得到以下结果

  • margin-top: 20,
  • margin-right: 15,
  • 底部边距:30
  • 左边距:15

设置四个值后,顶部边距将获得第一个值,右边距将获得第二个值,底部边距将获得第三个值,左边距将获得第四个值。例如,my_pdf_instance.set_margins("20 15 30 5") 将得到以下结果

  • margin-top: 20,
  • margin-right: 15,
  • 底部边距:30
  • 左边距:5

如果设置了其他值(或非整数、字母、字符等),则每个边距将使用默认值 10

页面字体

PDF文档可以使用14种标准字体之一。这些是

枚举 字体名称 字体粗细 字体样式
Courier Courier 正常 正常
CourierBold Courier 粗体 正常
CourierBoldOblique Courier 粗体 斜体
CourierOblique Courier 正常 斜体
Helvetica Helvetica 正常 正常
HelveticaBold Helvetica 粗体 正常
HelveticaBoldOblique Helvetica 粗体 斜体
HelveticaOblique Helvetica 正常 斜体
Symbol Symbol 正常 正常
TimesBold Times 粗体 正常
TimesBoldItalic Times 粗体 斜体
TimesItalic Times 正常 斜体
TimesRoman Times 正常 正常
ZapfDingbats Zapf Dingbats 正常 正常

例如,my_pdf_instance.set_font(FontsStandard::TimesRoman)。如果没有设置字体,则默认字体为 Helvetica

PDF字典条目

PDF字典条目是您在PDF阅读器中选择“文档属性”时可以看到的名称和值对。字典条目区分大小写,有一些保留名称。

保留字典条目

  • 标题
  • 作者
  • 主题
  • 关键词

这些 必须 大写。 PDF Composer 自动将保留名称转换为大写。所有其他内容将保持原样。

PDF Composer 中,标题条目是一个特殊情况。作为PDF生成过程的一部分,YAML文档中的 title 值将自动插入到HTML模板中使用的 <title> 标签中。因此,字典条目被填充。如果没有找到YAML值,则使用源文件的文件名代替。

不允许空条目。如果没有找到相应的YAML条目,则不会将空条目添加到PDF文档中。

例如,如果您想设置一个名为 Language 的字典条目,并将其设置为YAML文档中不存在的YAML条目,则 PDF Composer 不会创建空条目。

设置字典条目的示例

PDFDocInfoEntry {
    doc_info_entry: "Subject",
    yaml_entry: "description",
}

doc_info_entry 是PDF字典条目。

yaml_entry 是将分配给字典条目的YAML值。

YAML Markdown占位符替换

在 YAML 文档的 Markdown 部分中可以进行简单的替换。这可以通过在 Markdown 部分使用 {{my_yaml_value}} 实现。

示例

---

# Front Matter (YAML)

author: "Richard"

---

The author of this document is {{author}}.

结果将是: 本文档的作者是 Richard.

如果找不到 YAML 值,则替换占位符将保留在生成的输出中。

示例

---

# Front Matter (YAML)

author: "Richard"

---

The author of this document is {{name}}.

结果将是: 本文档的作者是 {{name}}.

示例用法

假设你已经安装并运行了 Rust(测试版本 1.77+)并且已经运行了 cargo add pdf_composer 来安装 PDF Composer 包,然后你可以开始了。

use pdf_composer::{FontsStandard, PaperOrientation, PaperSize, PDFComposer, PDFDocInfoEntry, PDFVersion};

// Create a new PDFComposer instance
let mut my_pdf_instance = PDFComposer::new();

// Add some paths. Relative paths
let paths = vec![
    PathBuf::from("source_mds/file_01.md"),
    PathBuf::from("source_mds/file_02.md")
];
my_pdf_instance.add_source_files(paths);

// PDF version (not the version of the document, but the Adobe (formerly) PDF format version)
my_pdf_instance.set_pdf_version(PDFVersion::V2_0);

// Output directory for the generated PDFs
my_pdf_instance.set_output_directory("example_pdfs");

// Set the paper size
my_pdf_instance.set_paper_size(PaperSize::A5);

// Set the paper orientation
my_pdf_instance.set_orientation(PaperOrientation::Landscape);

// Set the page margins
my_pdf_instance.set_margins("20");

// Set font
my_pdf_instance.set_font(FontsStandard::TimesRoman);

// Metadata for the PDFs
let author_entry = PDFDocInfoEntry {
    doc_info_entry: "Author",
    yaml_entry: "author",
};
let keywords_entry = PDFDocInfoEntry {
    doc_info_entry: "Keywords",
    yaml_entry: "keywords",
};
let subject_entry = PDFDocInfoEntry {
    doc_info_entry: "Subject",
    yaml_entry: "description",
};
let language_entry = PDFDocInfoEntry {
    doc_info_entry: "Language",
    yaml_entry: "language",
};
my_pdf_instance.set_doc_info_entry(author_entry);
my_pdf_instance.set_doc_info_entry(keywords_entry);
my_pdf_instance.set_doc_info_entry(subject_entry);
my_pdf_instance.set_doc_info_entry(language_entry);

// Generate the PDF(s)
my_pdf_instance.generate_pdfs();

许可证

该项目根据 MIT 许可证和 Apache 许可证(版本 2.0)的条款进行许可。

未来计划

一些想法,但不限于

  • HTML 模板
  • 漂亮的默认打印样式表
  • 允许直接使用字符串值作为 PDF 字典条目,而无需首先使用 YAML 值
  • 将多个 YAML 前置文档合并为一个生成的 PDF 文档
  • 分页和页码

依赖项

~31–45MB
~738K SLoC