pub struct PdfDocument {
pub fonts: FontList,
pub document_id: String,
pub metadata: PdfMetadata,
pub bookmarks: HashMap<usize, String>,
/* private fields */
}
Expand description
PDF document
Fields§
§fonts: FontList
Fonts used in this document
document_id: String
Document ID. Must be changed if the document is loaded / parsed from a file
metadata: PdfMetadata
Metadata for this document
bookmarks: HashMap<usize, String>
The bookmarks in the document. A HashMap<Page Number, Bookmark Name>
Implementations§
Source§impl PdfDocument
impl PdfDocument
Sourcepub fn new<S1, S2>(
document_title: S1,
initial_page_width: Mm,
initial_page_height: Mm,
initial_layer_name: S2,
) -> (PdfDocumentReference, PdfPageIndex, PdfLayerIndex)
pub fn new<S1, S2>( document_title: S1, initial_page_width: Mm, initial_page_height: Mm, initial_layer_name: S2, ) -> (PdfDocumentReference, PdfPageIndex, PdfLayerIndex)
Creates a new PDF document
Examples found in repository?
examples/bookmark.rs (line 8)
7fn main() {
8 let (doc, page1, _) = PdfDocument::new("printpdf page test", Mm(210.0), Mm(297.0), "Layer 1");
9 doc.add_bookmark("This is a bookmark", page1);
10
11 let (page2, _) = doc.add_page(Mm(297.0), Mm(210.0), "Page 2, Layer 1");
12 let _ = doc.get_page(page2).add_layer("Layer 3");
13 doc.add_bookmark("This is another bookmark", page2);
14
15 // If this is successful, you should see a PDF with two blank A4 pages and 2 bookmarks
16 doc.save(&mut BufWriter::new(
17 File::create("test_bookmark.pdf").unwrap(),
18 ))
19 .unwrap();
20}
More examples
examples/no_icc.rs (line 16)
10fn main() {
11 // This code creates the most minimal PDF file with 1.2 KB
12 // Currently, fonts need to use an embedded font, so if you need to write something, the file size
13 // will still be bloated (because of the embedded font)
14 // Also, OCG content is still enabled, even if you disable it here.
15 let (mut doc, _page1, _layer1) =
16 PdfDocument::new("printpdf no_icc test", Mm(297.0), Mm(210.0), "Layer 1");
17 doc = doc.with_conformance(PdfConformance::Custom(CustomPdfConformance {
18 requires_icc_profile: false,
19 requires_xmp_metadata: false,
20 ..Default::default()
21 }));
22
23 doc.save(&mut BufWriter::new(
24 File::create("test_no_icc.pdf").unwrap(),
25 ))
26 .unwrap();
27}
examples/page.rs (line 11)
7fn main() {
8 // To prevent empty documents, you must specify at least one page with one layer
9 // You can later on add more pages with the add_page() function
10 // You also have to specify the title of the PDF and the document creator
11 let (doc, _, _) = PdfDocument::new("printpdf page test", Mm(210.0), Mm(297.0), "Layer 1");
12
13 // You can add more pages and layers to the PDF.
14 // Just make sure you don't lose the references, otherwise, you can't add things to the layer anymore
15 let (page2, _) = doc.add_page(Mm(297.0), Mm(210.0), "Page 2, Layer 1");
16 let _ = doc.get_page(page2).add_layer("Layer 3");
17
18 // If this is successful, you should see a PDF with two blank A4 pages
19 doc.save(&mut BufWriter::new(File::create("test_pages.pdf").unwrap()))
20 .unwrap();
21}
examples/rect.rs (line 10)
8fn main() {
9 let (doc, page1, layer1) =
10 PdfDocument::new("printpdf rect test", Mm(210.0), Mm(297.0), "Layer 1");
11 let current_layer = doc.get_page(page1).get_layer(layer1);
12
13 let rect = Rect::new(Mm(30.), Mm(250.), Mm(200.), Mm(290.));
14
15 current_layer.add_rect(rect);
16
17 let rect = Rect::new(Mm(50.), Mm(180.), Mm(120.), Mm(290.))
18 .with_mode(PaintMode::Clip)
19 .with_winding(WindingOrder::EvenOdd);
20
21 current_layer.add_rect(rect);
22
23 let mut font_reader =
24 std::io::Cursor::new(include_bytes!("../assets/fonts/RobotoMedium.ttf").as_ref());
25 let font = doc.add_external_font(&mut font_reader).unwrap();
26
27 current_layer.use_text("hello world", 100.0, Mm(10.0), Mm(200.0), &font);
28 doc.save(&mut BufWriter::new(File::create("test_rect.pdf").unwrap()))
29 .unwrap();
30}
examples/hyperlink.rs (line 9)
7fn main() {
8 let (doc, page1, layer1) =
9 PdfDocument::new("printpdf graphics test", Mm(595.276), Mm(841.89), "Layer 1");
10 let current_layer = doc.get_page(page1).get_layer(layer1);
11
12 let text = "Lorem ipsum";
13 let font = doc.add_builtin_font(BuiltinFont::TimesBoldItalic).unwrap();
14 current_layer.use_text(text, 48.0, Mm(10.0), Mm(200.0), &font);
15
16 current_layer.add_link_annotation(LinkAnnotation::new(
17 printpdf::Rect::new(Mm(10.0), Mm(200.0), Mm(100.0), Mm(212.0)),
18 Some(printpdf::BorderArray::default()),
19 Some(printpdf::ColorArray::default()),
20 printpdf::Actions::uri("https://www.google.com/".to_string()),
21 Some(printpdf::HighlightingMode::Invert),
22 ));
23
24 doc.save(&mut BufWriter::new(
25 File::create("test_hyperlink.pdf").unwrap(),
26 ))
27 .unwrap();
28}
examples/svg.rs (line 10)
8fn main() {
9 let (doc, page1, layer1) =
10 PdfDocument::new("printpdf graphics test", Mm(210.0), Mm(297.0), "Layer 1");
11 let current_layer = doc.get_page(page1).get_layer(layer1);
12 let svg = Svg::parse(SVG).unwrap();
13
14 let rotation_center_x = Px((svg.width.0 as f32 / 2.0) as usize);
15 let rotation_center_y = Px((svg.height.0 as f32 / 2.0) as usize);
16
17 let reference = svg.into_xobject(¤t_layer);
18
19 for i in 0..10 {
20 reference.clone().add_to_layer(
21 ¤t_layer,
22 SvgTransform {
23 rotate: Some(SvgRotation {
24 angle_ccw_degrees: i as f32 * 36.0,
25 rotation_center_x: rotation_center_x.into_pt(300.0),
26 rotation_center_y: rotation_center_y.into_pt(300.0),
27 }),
28 translate_x: Some(Mm(i as f32 * 20.0 % 50.0).into()),
29 translate_y: Some(Mm(i as f32 * 30.0).into()),
30 ..Default::default()
31 },
32 );
33 }
34
35 let pdf_bytes = doc.save_to_bytes().unwrap();
36 std::fs::write("test_svg.pdf", &pdf_bytes).unwrap();
37}
Additional examples can be found in:
pub fn empty<S: Into<String>>(document_title: S) -> PdfDocumentReference
Trait Implementations§
Source§impl Clone for PdfDocument
impl Clone for PdfDocument
Source§fn clone(&self) -> PdfDocument
fn clone(&self) -> PdfDocument
Returns a copy of the value. Read more
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
Performs copy-assignment from
source
. Read moreAuto Trait Implementations§
impl Freeze for PdfDocument
impl !RefUnwindSafe for PdfDocument
impl !Send for PdfDocument
impl !Sync for PdfDocument
impl Unpin for PdfDocument
impl !UnwindSafe for PdfDocument
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
Converts
self
into a Left
variant of Either<Self, Self>
if into_left
is true
.
Converts self
into a Right
variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
Converts
self
into a Left
variant of Either<Self, Self>
if into_left(&self)
returns true
.
Converts self
into a Right
variant of Either<Self, Self>
otherwise. Read more