Struct PdfDocument

Source
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

Source

pub fn new<S1, S2>( document_title: S1, initial_page_width: Mm, initial_page_height: Mm, initial_layer_name: S2, ) -> (PdfDocumentReference, PdfPageIndex, PdfLayerIndex)
where S1: Into<String>, S2: Into<String>,

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
Hide additional 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(&current_layer);
18
19    for i in 0..10 {
20        reference.clone().add_to_layer(
21            &current_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}
Source

pub fn empty<S: Into<String>>(document_title: S) -> PdfDocumentReference

Trait Implementations§

Source§

impl Clone for PdfDocument

Source§

fn clone(&self) -> PdfDocument

Returns a copy of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for PdfDocument

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> Finish for T

Source§

fn finish(self)

Does nothing but move self, equivalent to drop.
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> IntoEither for T

Source§

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 more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

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
Source§

impl<T> Pointable for T

Source§

const ALIGN: usize

The alignment of pointer.
Source§

type Init = T

The type for initializers.
Source§

unsafe fn init(init: <T as Pointable>::Init) -> usize

Initializes a with the given initializer. Read more
Source§

unsafe fn deref<'a>(ptr: usize) -> &'a T

Dereferences the given pointer. Read more
Source§

unsafe fn deref_mut<'a>(ptr: usize) -> &'a mut T

Mutably dereferences the given pointer. Read more
Source§

unsafe fn drop(ptr: usize)

Drops the object pointed to by the given pointer. Read more
Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.