Struct printpdf::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)
7
8
9
10
11
12
13
14
15
16
17
18
19
20
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();
}
More examples
Hide additional examples
examples/no_icc.rs (line 16)
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
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();
}
examples/page.rs (line 11)
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
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();
}
examples/rect.rs (line 10)
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
fn main() {
    let (doc, page1, layer1) =
        PdfDocument::new("printpdf rect test", Mm(210.0), Mm(297.0), "Layer 1");
    let current_layer = doc.get_page(page1).get_layer(layer1);

    let rect = Rect::new(Mm(30.), Mm(250.), Mm(200.), Mm(290.));

    current_layer.add_rect(rect);

    let rect = Rect::new(Mm(50.), Mm(180.), Mm(120.), Mm(290.))
        .with_mode(PaintMode::Clip)
        .with_winding(WindingOrder::EvenOdd);

    current_layer.add_rect(rect);

    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();

    current_layer.use_text("hello world", 100.0, Mm(10.0), Mm(200.0), &font);
    doc.save(&mut BufWriter::new(File::create("test_rect.pdf").unwrap()))
        .unwrap();
}
examples/hyperlink.rs (line 9)
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
fn main() {
    let (doc, page1, layer1) =
        PdfDocument::new("printpdf graphics test", Mm(595.276), Mm(841.89), "Layer 1");
    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);

    current_layer.add_link_annotation(LinkAnnotation::new(
        printpdf::Rect::new(Mm(10.0), Mm(200.0), Mm(100.0), Mm(212.0)),
        Some(printpdf::BorderArray::default()),
        Some(printpdf::ColorArray::default()),
        printpdf::Actions::uri("https://www.google.com/".to_string()),
        Some(printpdf::HighlightingMode::Invert),
    ));

    doc.save(&mut BufWriter::new(
        File::create("test_hyperlink.pdf").unwrap(),
    ))
    .unwrap();
}
examples/svg.rs (line 10)
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
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);
    let svg = Svg::parse(SVG).unwrap();

    let rotation_center_x = Px((svg.width.0 as f32 / 2.0) as usize);
    let rotation_center_y = Px((svg.height.0 as f32 / 2.0) as usize);

    let reference = svg.into_xobject(&current_layer);

    for i in 0..10 {
        reference.clone().add_to_layer(
            &current_layer,
            SvgTransform {
                rotate: Some(SvgRotation {
                    angle_ccw_degrees: i as f32 * 36.0,
                    rotation_center_x: rotation_center_x.into_pt(300.0),
                    rotation_center_y: rotation_center_y.into_pt(300.0),
                }),
                translate_x: Some(Mm(i as f32 * 20.0 % 50.0).into()),
                translate_y: Some(Mm(i as f32 * 30.0).into()),
                ..Default::default()
            },
        );
    }

    let pdf_bytes = doc.save_to_bytes().unwrap();
    std::fs::write("test_svg.pdf", &pdf_bytes).unwrap();
}
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> 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.

§

impl<T> Pointable for T

§

const ALIGN: usize = _

The alignment of pointer.
§

type Init = T

The type for initializers.
§

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

Initializes a with the given initializer. Read more
§

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

Dereferences the given pointer. Read more
§

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

Mutably dereferences the given pointer. Read more
§

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,

§

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>,

§

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>,

§

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.