英文:
Unsure how to format my pdf with the "Printpdf 0.5.3" crate in Rust
问题
我相对于Rust编程比较新,需要一点关于如何实现我的项目的方向。
目前,我正在尝试构建一个命令行工具,它接收一个人的名字、他们申请的职位、他们申请的公司以及该公司的位置,然后自动地将其求职信内容格式化成PDF。最终,我想将其制作成一个图形用户界面工具,可以发送给我的朋友,让他们可以在他们的Windows/Mac机器上使用它。
问题是,我目前正在使用"Printpdf"库来生成文档本身。正如下面的图片所示,文本在达到行末时并不会自动换行/自动断行/自动格式化。不幸的是,我不能简单地将其分割成不同的字符串变量,因为程序需要能够添加任意用户输入并返回一个完美格式化的PDF文件。
如何去格式化这个文档?我应该使用不是"printpdf"的库吗?还是可以使用其他方法让文本按我想要的方式显示?
以下是Rust代码和PDF输出的图片。
use std::io;
use printpdf::*;
use std::fs::File;
use std::io::BufWriter;
fn main() {
println!("Enter: Position");
let mut position = String::new();
io::stdin()
.read_line(&mut position)
.expect("failed to read");
println!("Enter: Company Name");
let mut coname = String::new();
io::stdin()
.read_line(&mut coname)
.expect("failed to read");
println!("Enter: Company Location");
let mut location = String::new();
io::stdin()
.read_line(&mut location)
.expect("failed to read");
let ntxt = " ";
let sample = "This is some random Sample text, This text should eventually be a user input. Currently, this text is not a user input. this text is supposed to be a text of long string data that will eventually be added to the document via user input";
let (doc, page1, layer1) = PdfDocument::new("PDF_Document_title", Mm(216.0), Mm(279.0), "Layer 1");
let current_layer = doc.get_page(page1).get_layer(layer1);
let font = doc.add_external_font(File::open("./fonts/TNR-Regular.ttf").unwrap()).unwrap();
current_layer.use_text(position.clone(), 14.0, Mm(25.0), Mm(250.0), &font);
current_layer.use_text(coname.clone(), 14.0, Mm(25.0), Mm(240.0), &font);
current_layer.use_text(location.clone(), 14.0, Mm(25.0), Mm(230.0), &font);
current_layer.use_text(ntxt.clone(), 14.0, Mm(25.0), Mm(220.0), &font);
current_layer.begin_text_section();
current_layer.set_font(&font, 14.0);
current_layer.set_text_cursor(Mm(25.0), Mm(210.0));
// write one line, but write text2 in superscript
current_layer.write_text(sample.clone(), &font);
current_layer.end_text_section();
doc.save(&mut BufWriter::new(File::create("test_working.pdf").unwrap())).unwrap();
}
我真的不知道从哪里开始解决这个问题。
我尝试查看"Textwrap"库的文档,但它对我来说看起来像一个谜。我还查看了"Printpdf"的文档,但它说它不支持格式化和对齐。
英文:
I am relatively new to programming in rust and need a bit of direction on how to implement my project.
Currently, I am trying to build a CLI tool that takes a persons name, position they are applying too, company they are applying too and the location of that company and auto-magically formats a PDF with the contents of their cover letter. eventually, i want to make this into a GUI tool that I can send to my friends so they can use it on their windows/mac machines.
the problem right now is that I am using the "Printpdf" crate to generate the document itself. as you can see with the picture below, the text doesn't automatically wrap/line-break/format when it reaches the end of the line. unfortunately, I can not simply break this up into different string variables as the program needs to be able to add arbitrary user input and return a perfectly formatted PDF file.
How to I go about formatting this document? should I use a crate that isn't "printpdf"? or can I use something else to get the text to behave the way I want?
here is the rust code and a picture of what the PDF outputs.
use std::io;
use printpdf::*;
use std::fs::File;
use std::io::BufWriter;
fn main() {
println!("Enter: Position");
let mut position = String::new();
io:: stdin()
.read_line(&mut position)
.expect("failed to read");
println!("Enter: Company Name");
let mut coname = String::new();
io:: stdin()
.read_line(&mut coname)
.expect("failed to read");
println!("Enter: Company Location");
let mut location = String::new();
io:: stdin()
.read_line(&mut location)
.expect("failed to read");
let ntxt = " ";
let sample = "This is some random Sample text, This text should eventaully be a user input. Currently, this text is not a user input. this text is supposed to be a text of long string data that will eventually be added to the document via user input";
let (doc, page1, layer1) = PdfDocument::new("PDF_Document_title", Mm(216.0), Mm(279.0), "Layer 1");
let current_layer = doc.get_page(page1).get_layer(layer1);
let font = doc.add_external_font(File::open("./fonts/TNR-Regular.ttf").unwrap()).unwrap();
current_layer.use_text(position.clone(), 14.0, Mm(25.0), Mm(250.0), &font);
current_layer.use_text(coname.clone(), 14.0, Mm(25.0), Mm(240.0), &font);
current_layer.use_text(location.clone(), 14.0, Mm(25.0), Mm(230.0), &font);
current_layer.use_text(ntxt.clone(), 14.0, Mm(25.0), Mm(220.0), &font);
current_layer.begin_text_section();
current_layer.set_font(&font, 14.0);
current_layer.set_text_cursor(Mm(25.0), Mm(210.0));
// write one line, but write text2 in superscript
current_layer.write_text(sample.clone(), &font);
current_layer.end_text_section();
doc.save(&mut BufWriter::new(File::create("test_working.pdf").unwrap())).unwrap();
}
I don't really know where to start with fixing this issue.
I have tried to look at the documentation for the crate "textwrap" but it looks like an utter enigma too me. I also looked at the documentation for "printpdf" but it says it doesn't support formatting and alignment.
答案1
得分: 0
这个代码的主要作用如下:
- 加载
font_data
作为字节的向量Vec<u8>
;glyph_brush_layout
和printpdf
都会独立地将这个字体作为字节切片&[u8]
进行处理。 - 使用
glyph_brush_layout
来计算字形的位置*(示例是在 README 中适配的),具体是在 160 毫米宽的区域内获取垂直y
位置。 - 将字形根据这些
y
位置进行分组(它们是每行文本基线的垂直位置),以及它们在文本中的索引(其中文本 =sample
)。这里使用了itertools
使这一过程更容易。 - 取每个分组中的第一个字形(
group.next().unwrap().0
),获取其索引(以及垂直位置)。这些索引被收集到一个向量中。 - 遍历这个向量,将文本写入 PDF。为了拆分文本,创建了一个可预览的迭代器,这样我们可以为当前索引和下一个(预览的)索引获取文本
sample
的切片。由于printpdf
和glyph_brush_layout
处理布局的方式不同,所以需要进行一些垂直偏移和转换。
- 这里的假设是一个字形等于文本中的一个字符,即
assert_eq!(glyphs.len(), sample.chars().count());
。如果不是这种情况,也许你需要考虑一次直接定位一个字形。
此外,代码还包含了与用户输入相关的内容以及 PDF 的创建和保存部分。
英文:
There is probably a more sane way to do this, but this works:
- We load the
font_data
as a Vector of bytesVec<u8>
; bothglyph_brush_layout
andprintpdf
then independently work with that font as a slice of bytes&[u8]
. - We use
glyph_brush_layout
to calculate the position of the glyphs* (I just adapted the example in the README), specifically getting the verticaly
positions in a box 160mm wide. - We group the glyphs on those
y
positions (they are the vertical positions of the baseline of each line of text), along with their index into the text (where text =sample
). I useditertools
to make this easier. - We take just the first glyph in each group (
group.next().unwrap().0
), getting its index (and y position). These indexes are the positions at which we will split the text into individual lines. These are collected into a Vector. - We loop over the Vector, writing the text to the PDF. To split the text we create a peekable iterator, so we can take the slice of the text
sample
for the current index and the next (peeked) index. Because of the different ways thatprintpdf
andglyph_brush_layout
deal with layout, we need to do some vertical offsets and conversions.
*The assumption here is that one glyph equals one character in the text, i.e. assert_eq!(glyphs.len(), sample.chars().count());
. If that's not the case, maybe you want to consider positioning one glyph at a time, directly.
main.rs
use printpdf::*;
use std::fs::File;
use std::io::BufWriter;
use std::io::{self, Read};
fn main() {
println!("Enter: Position");
let mut position = String::new();
io::stdin()
.read_line(&mut position)
.expect("failed to read");
println!("Enter: Company Name");
let mut coname = String::new();
io::stdin().read_line(&mut coname).expect("failed to read");
println!("Enter: Company Location");
let mut location = String::new();
io::stdin()
.read_line(&mut location)
.expect("failed to read");
let ntxt = " ";
let sample = "This is some random Sample text, This text should eventaully be a user input. Currently, this text is not a user input. this text is supposed to be a text of long string data that will eventually be added to the document via user input";
let (doc, page1, layer1) =
PdfDocument::new("PDF_Document_title", Mm(216.0), Mm(279.0), "Layer 1");
let current_layer = doc.get_page(page1).get_layer(layer1);
// load the font data for the font "Times New Roman"
let font_data = {
let mut font_file = File::open("./times-new-roman.ttf").unwrap();
let mut font_data = Vec::with_capacity(font_file.metadata().unwrap().len() as usize);
font_file.read_to_end(&mut font_data).unwrap();
font_data
};
// load the font reference for glyph_brush_layout
let gbl_font = glyph_brush_layout::ab_glyph::FontRef::try_from_slice(&font_data).unwrap();
// put it into a slice of glyph_brush_layout font references
let gbl_fonts = &[gbl_font];
// load the font reference for printpdf
let font = doc.add_external_font(font_data.as_slice()).unwrap();
current_layer.use_text(position.clone(), 14.0, Mm(25.0), Mm(250.0), &font);
current_layer.use_text(coname.clone(), 14.0, Mm(25.0), Mm(240.0), &font);
current_layer.use_text(location.clone(), 14.0, Mm(25.0), Mm(230.0), &font);
current_layer.use_text(ntxt, 14.0, Mm(25.0), Mm(220.0), &font);
// calculate the glyph positions using glyph_brush_layout
use glyph_brush_layout::ab_glyph::Font;
use glyph_brush_layout::GlyphPositioner;
let glyphs = glyph_brush_layout::Layout::default().calculate_glyphs(
gbl_fonts,
&glyph_brush_layout::SectionGeometry {
// width 160mm = 210mm - 2 * 25mm margins; height unbounded
bounds: (mm_to_px(160.0), f32::INFINITY),
..Default::default()
},
&[glyph_brush_layout::SectionText {
text: sample,
scale: gbl_fonts[0].pt_to_px_scale(14.0).unwrap(),
font_id: glyph_brush_layout::FontId(0),
}],
);
// make sure the number of glyphs matches the number of chars in the sample text
assert_eq!(glyphs.len(), sample.chars().count());
// group the glyphs by y position
use itertools::Itertools;
let line_starts = glyphs
.iter()
.enumerate() // enumerate will give us the start index into the sample text of the start of the line
.group_by(|(_, glyph)| glyph.glyph.position.y) // group by "y" which is effectively equivalent to the index of the line
.into_iter()
.map(|(y, mut group)| (y, group.next().unwrap().0))
.collect::<Vec<_>>();
// get the minimum y position
// you could get the max a similar way, if you needed to calculate the vertical size of the text,
// for example if you wanted to lay out text below it
let min = glyphs
.iter()
.map(|glyph| glyph.glyph.position.y)
.fold(f32::INFINITY, |a, b| a.min(b));
// we need a peekable iterator so we can see where the next line starts
let mut iter = line_starts.iter().peekable();
// iterate over the line_starts and draw the text
loop {
// get the next line start, if there is none then we break out of the loop
let Some((y, start)) = iter.next() else {
break;
};
// peek into the line start after that to get the end index,
// if there is none (we're at the last line in the loop), then we use the length of the sample text
let end = if let Some((_, end)) = iter.peek() {
*end
} else {
sample.chars().count()
};
// slice up the text
// if you know you're only dealing with ASCII characters you can simplify this as
// `let line = &sample[*start..end];`
// which saves on an allocation to a String;
// or you can use char_indices to get the byte indices and slice that way
let line = sample
.chars()
.skip(*start)
.take(end - start)
.collect::<String>();
// draw the text
current_layer.use_text(
line.trim(),
14.0,
Mm(25.0),
// printpdf up = y positive, but glyph_brush_layout down = y positive
Mm(210.0 + px_to_mm(min) - px_to_mm(*y)),
&font,
);
}
doc.save(&mut BufWriter::new(
File::create("test_working.pdf").unwrap(),
))
.unwrap();
}
/// glyph_brush_layout deals with f32 pixels, but printpdf deals with f64 mm.
fn px_to_mm(px: f32) -> f64 {
px as f64 * 3175.0 / 12000.0
}
/// printpdf deals with f64 mm, but glyph_brush_layout deals with f32 pixels.
fn mm_to_px(mm: f64) -> f32 {
(mm * 12000.0 / 3175.0) as f32
}
cargo.toml
[package]
name = "generate-pdf"
version = "0.1.0"
edition = "2021"
[dependencies]
glyph_brush_layout = "0.2.3"
itertools = "0.10.5"
printpdf = "0.5.3"
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论