Compress embedded font streams in the PDF writer

This commit is contained in:
gloamxun committed 2026-10-19 22:15:27 +08:00
1 parent 369472e7f7
commit 4735d75c1f
80 files changed
+9119 -5

No files matched your search

+20
View File
@@ -0,0 +1,20 @@
extern crate printpdf;
use printpdf::*;
use std::fs::File;
use std::io::BufWriter;
fn main() {
let (doc, page1, _) = PdfDocument::new("printpdf page test", Mm(210.0), Mm(297.0), "Layer 1");
doc.add_bookmark("This is a bookmark", page1);
let (page2, _) = doc.add_page(Mm(297.0), Mm(210.0), "Page 2, Layer 1");
let _ = doc.get_page(page2).add_layer("Layer 3");
doc.add_bookmark("This is another bookmark", page2);
// If this is successful, you should see a PDF with two blank A4 pages and 2 bookmarks
doc.save(&mut BufWriter::new(
File::create("test_bookmark.pdf").unwrap(),
))
.unwrap();
}
+23
View File
@@ -0,0 +1,23 @@
//! Example on how to use a built-in font in a PDF
extern crate printpdf;
use printpdf::*;
use std::fs::File;
use std::io::BufWriter;
fn main() {
let (mut doc, page1, layer1) = PdfDocument::new("PDF_Document_title", Mm(500.0), Mm(300.0), "Layer 1");
doc = doc.with_conformance(PdfConformance::Custom(CustomPdfConformance {
requires_icc_profile: false,
requires_xmp_metadata: false,
.. Default::default()
}));
let current_layer = doc.get_page(page1).get_layer(layer1);
let text = "Lorem ipsum";
let font = doc.add_builtin_font(BuiltinFont::TimesBoldItalic).unwrap();
current_layer.use_text(text, 48.0, Mm(10.0), Mm(200.0), &font);
doc.save(&mut BufWriter::new(File::create("test_builtin_fonts.pdf").unwrap())).unwrap();
}
+44
View File
@@ -0,0 +1,44 @@
extern crate printpdf;
use printpdf::*;
use std::fs::File;
use std::io::BufWriter;
fn main() {
use printpdf::utils::{calculate_points_for_circle, calculate_points_for_rect};
let (doc, page1, layer1) = PdfDocument::new("printpdf circle test", Mm(210.0), Mm(297.0), "Layer 1");
let current_layer = doc.get_page(page1).get_layer(layer1);
let radius = Pt(40.0);
let offset_x = Pt(10.0);
let offset_y = Pt(50.0);
let line = Line {
points: calculate_points_for_circle(radius, offset_x, offset_y),
is_closed: true,
has_fill: true,
has_stroke: true,
is_clipping_path: false
};
current_layer.add_shape(line);
let scale_x_rect = Pt(40.0);
let scale_y_rect = Pt(10.0);
let offset_x_rect = Pt(20.0);
let offset_y_rect = Pt(5.0);
let line = Line {
points: calculate_points_for_rect(scale_x_rect, scale_y_rect, offset_x_rect, offset_y_rect),
is_closed: true,
has_fill: true,
has_stroke: true,
is_clipping_path: false
};
current_layer.add_shape(line);
doc.save(&mut BufWriter::new(File::create("test_circle.pdf").unwrap())).unwrap();
}
+60
View File
@@ -0,0 +1,60 @@
extern crate printpdf;
use printpdf::*;
use std::fs::File;
use std::io::BufWriter;
fn main() {
let (doc, page1, layer1) = PdfDocument::new("PDF_Document_title", Mm(500.0), Mm(300.0), "Layer 1");
let current_layer = doc.get_page(page1).get_layer(layer1);
let text = "Lorem ipsum";
let text2 = "dolor sit amet";
let mut font_reader = std::io::Cursor::new(include_bytes!("../assets/fonts/RobotoMedium.ttf").as_ref());
let font = doc.add_external_font(&mut font_reader).unwrap();
// `use_text` is a wrapper around making a simple string
current_layer.use_text(text, 48.0, Mm(10.0), Mm(200.0), &font);
// text fill color = blue
let blue = Rgb::new(13.0 / 256.0, 71.0 / 256.0, 161.0 / 256.0, None);
let orange = Rgb::new(244.0 / 256.0, 67.0 / 256.0, 54.0 / 256.0, None);
current_layer.set_fill_color(Color::Rgb(blue));
current_layer.set_outline_color(Color::Rgb(orange));
// For more complex layout of text, you can use functions
// defined on the PdfLayerReference
// Make sure to wrap your commands
// in a `begin_text_section()` and `end_text_section()` wrapper
current_layer.begin_text_section();
// setup the general fonts.
// see the docs for these functions for details
current_layer.set_font(&font, 33.0);
current_layer.set_text_cursor(Mm(10.0), Mm(100.0));
current_layer.set_line_height(33.0);
current_layer.set_word_spacing(3000.0);
current_layer.set_character_spacing(10.0);
// write two lines (one line break)
current_layer.write_text(text, &font);
current_layer.add_line_break();
current_layer.write_text(text2, &font);
current_layer.add_line_break();
current_layer.set_text_rendering_mode(TextRenderingMode::FillStroke);
current_layer.set_character_spacing(0.0);
current_layer.set_text_matrix(TextMatrix::Rotate(10.0));
// write one line, but write text2 in superscript
current_layer.write_text(text, &font);
current_layer.set_line_offset(10.0);
current_layer.set_text_rendering_mode(TextRenderingMode::Stroke);
current_layer.set_font(&font, 18.0);
current_layer.write_text(text2, &font);
current_layer.end_text_section();
doc.save(&mut BufWriter::new(File::create("test_fonts.pdf").unwrap())).unwrap();
}
+74
View File
@@ -0,0 +1,74 @@
extern crate printpdf;
use printpdf::*;
use std::fs::File;
use std::io::BufWriter;
use std::iter::FromIterator;
fn main() {
let (doc, page1, layer1) = PdfDocument::new("printpdf graphics test", Mm(297.0), Mm(297.0), "Layer 1");
let current_layer = doc.get_page(page1).get_layer(layer1);
// Quadratic shape. The "false" determines if the next (following)
// point is a bezier handle (for curves)
// If you want holes, simply reorder the winding of the points to be
// counterclockwise instead of clockwise.
let points1 = vec![(Point::new(Mm(100.0), Mm(100.0)), false),
(Point::new(Mm(100.0), Mm(200.0)), false),
(Point::new(Mm(300.0), Mm(200.0)), false),
(Point::new(Mm(300.0), Mm(100.0)), false)];
// Is the shape stroked? Is the shape closed? Is the shape filled?
let line1 = Line {
points: points1,
is_closed: true,
has_fill: true,
has_stroke: true,
is_clipping_path: false,
};
// Triangle shape
// Note: Line is invisible by default, the previous method of
// constructing a line is recommended!
let mut line2 = Line::from_iter(vec![
(Point::new(Mm(150.0), Mm(150.0)), false),
(Point::new(Mm(150.0), Mm(250.0)), false),
(Point::new(Mm(350.0), Mm(250.0)), false)]);
line2.set_closed(false);
line2.set_stroke(true);
line2.set_fill(false);
line2.set_as_clipping_path(false);
let fill_color = Color::Cmyk(Cmyk::new(0.0, 0.23, 0.0, 0.0, None));
let outline_color = Color::Rgb(Rgb::new(0.75, 1.0, 0.64, None));
let mut dash_pattern = LineDashPattern::default();
dash_pattern.dash_1 = Some(20);
current_layer.set_fill_color(fill_color);
current_layer.set_outline_color(outline_color);
current_layer.set_outline_thickness(10.0);
// Draw first line
current_layer.add_shape(line1);
let fill_color_2 = Color::Cmyk(Cmyk::new(0.0, 0.0, 0.0, 0.0, None));
let outline_color_2 = Color::Greyscale(Greyscale::new(0.45, None));
// More advanced graphical options
current_layer.set_overprint_stroke(true);
current_layer.set_blend_mode(BlendMode::Seperable(SeperableBlendMode::Multiply));
current_layer.set_line_dash_pattern(dash_pattern);
current_layer.set_line_cap_style(LineCapStyle::Round);
current_layer.set_line_join_style(LineJoinStyle::Round);
current_layer.set_fill_color(fill_color_2);
current_layer.set_outline_color(outline_color_2);
current_layer.set_outline_thickness(15.0);
// draw second line
current_layer.add_shape(line2);
// If this is successful, you should see a PDF two shapes, one rectangle
// and a dotted line
doc.save(&mut BufWriter::new(File::create("test_graphics.pdf").unwrap())).unwrap();
}
+26
View File
@@ -0,0 +1,26 @@
extern crate printpdf;
use printpdf::*;
use std::io::Cursor;
use image::bmp::BmpDecoder;
use std::fs::File;
use std::io::BufWriter;
fn main() {
let (doc, page1, layer1) = PdfDocument::new("printpdf graphics test", Mm(210.0), Mm(297.0), "Layer 1");
let current_layer = doc.get_page(page1).get_layer(layer1);
// currently, the only reliable file formats are bmp/jpeg/png
// this is an issue of the image library, not a fault of printpdf
let image_bytes = include_bytes!("../assets/img/BMP_test.bmp");
let mut reader = Cursor::new(image_bytes.as_ref());
let decoder = BmpDecoder::new(&mut reader).unwrap();
let image2 = Image::try_from(decoder).unwrap();
// layer,
image2.add_to_layer(current_layer.clone(), None, None, None, None, None, None);
doc.save(&mut BufWriter::new(File::create("test_image.pdf").unwrap())).unwrap();
}
+24
View File
@@ -0,0 +1,24 @@
//! Example to demonstrate how to remove the default ICC profile
//! Look at the file size (compared to the other tests!)
extern crate printpdf;
use printpdf::*;
use std::fs::File;
use std::io::BufWriter;
fn main() {
// This code creates the most minimal PDF file with 1.2 KB
// Currently, fonts need to use an embedded font, so if you need to write something, the file size
// will still be bloated (because of the embedded font)
// Also, OCG content is still enabled, even if you disable it here.
let (mut doc, _page1, _layer1) = PdfDocument::new("printpdf no_icc test", Mm(297.0), Mm(210.0), "Layer 1");
doc = doc.with_conformance(PdfConformance::Custom(CustomPdfConformance {
requires_icc_profile: false,
requires_xmp_metadata: false,
.. Default::default()
}));
doc.save(&mut BufWriter::new(File::create("test_no_icc.pdf").unwrap())).unwrap();
}
+20
View File
@@ -0,0 +1,20 @@
extern crate printpdf;
use printpdf::*;
use std::fs::File;
use std::io::BufWriter;
fn main() {
// To prevent empty documents, you must specify at least one page with one layer
// You can later on add more pages with the add_page() function
// You also have to specify the title of the PDF and the document creator
let (doc, _, _) = PdfDocument::new("printpdf page test", Mm(210.0), Mm(297.0), "Layer 1");
// You can add more pages and layers to the PDF.
// Just make sure you don't lose the references, otherwise, you can't add things to the layer anymore
let (page2, _) = doc.add_page(Mm(297.0), Mm(210.0),"Page 2, Layer 1");
let _ = doc.get_page(page2).add_layer("Layer 3");
// If this is successful, you should see a PDF with two blank A4 pages
doc.save(&mut BufWriter::new(File::create("test_pages.pdf").unwrap())).unwrap();
}