fschutt / printpdf

An easy-to-use library for writing PDF in Rust
https://docs.rs/printpdf/
MIT License
777 stars 95 forks source link

The generated pdf contains just two characters up to 56.5MB #129

Open eksea opened 1 year ago

eksea commented 1 year ago

i create a pdf file with just two Chinese characters. But the file size is 56MB. The font i use is 'STHeiti Medium.ttc' on mac which size is 53MB.

fschutt commented 1 year ago

Yeah, it embeds the entire font without subsetting.

This could technically be solved with switching to subsetting fonts with allsorts and only using the glyphs that are being used.

eksea commented 1 year ago

Yes,i solved the problem with the help of allsorts finally.

dnlmlr commented 1 year ago

I implemented basic font subsetting which was merged in e412845dd5ca798a0e630ce3320ed2a5e6c1df0b . Since you also seem to have used allsorts, I would be curious if you have any comments / improvement ideas. Or even if you did it in a totally different way.

eksea commented 1 year ago

There's a font.rs in example. It read a font from file and then use it write text. I just use allsorts to generate a small font in memory, and then use the same api get a same font object, just like the example。

let small_font = subset_text(&provider, &all_text).unwrap();
let mut font_reader = std::io::Cursor::new(small_font);
let font = doc.add_external_font(&mut font_reader).unwrap();