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
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)
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
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(¤t_layer);
for i in 0..10 {
reference.clone().add_to_layer(
¤t_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();
}
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 !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