pub struct Pdf { /* private fields */ }
Expand description
The top-level object for writing a PDF.
A PDF file is created with the create
or new
methods.
Some metadata can be stored with set_foo
methods, and pages
are appended with the render_page
method.
Don’t forget to call finish
when done, to write the document
trailer, without it the written file won’t be a proper PDF.
Implementations§
Source§impl Pdf
impl Pdf
Sourcepub fn create(filename: &str) -> Result<Pdf>
pub fn create(filename: &str) -> Result<Pdf>
Create a new PDF document as a new file with given filename.
Examples found in repository?
14fn main() {
15 // Open our pdf document.
16 let mut document = Pdf::create("circles.pdf").unwrap();
17
18 // Add a 400x400 pt page.
19
20 // Render-page writes the pdf file structure for a page and
21 // creates a Canvas which is sent to the function that is the last
22 // argument of the render_page method.
23 // That function then puts content on the page by calling methods
24 // on the canvas.
25 document
26 .render_page(400.0, 400.0, |c| {
27 let (x, y) = (200.0, 200.0);
28 let r = 190.0;
29
30 // Set a wide black pen and stroke a circle
31 c.set_stroke_color(Color::rgb(0, 0, 0))?;
32 c.set_line_width(2.0)?;
33 c.circle(x, y, r)?;
34 c.stroke()?;
35
36 // Set a finer yellow pen and stroke a 200-sided polygon
37 c.set_stroke_color(Color::rgb(255, 230, 150))?;
38 c.set_line_width(1.0)?;
39 c.move_to(x + r, y)?;
40 let sides: u8 = 200;
41 for n in 1..sides {
42 let phi = f32::from(n) * 2.0 * PI / f32::from(sides);
43 c.line_to(x + r * phi.cos(), y + r * phi.sin())?;
44 }
45 c.close_and_stroke()
46 })
47 .unwrap();
48 document.finish().unwrap();
49}
More examples
10fn main() {
11 // Open our pdf document.
12 let mut document = Pdf::create("mandala.pdf").expect("Create PDF file");
13 let mut args = env::args().skip(1);
14 let n: u8 = args.next().map(|s| s.parse().expect("number")).unwrap_or(7);
15
16 // Render a page with something resembling a mandala on it.
17 document
18 .render_page(600.0, 600.0, |c| {
19 c.concat(Matrix::translate(300., 300.))?;
20 c.set_stroke_color(Color::gray(0))?;
21 let segment = 2. * PI / n as f32;
22 for _i in 0..n {
23 c.move_to(0., 33.5)?;
24 c.line_to(0., 250.)?;
25 let r = 99.;
26 c.circle(0., r, r * 1.25 * segment)?;
27 let d = 141.4;
28 let rr = 36.;
29 c.circle(0., d + rr, rr)?;
30 c.stroke()?;
31 c.concat(Matrix::rotate(segment))?;
32 }
33 c.concat(Matrix::rotate(segment / 2.))?;
34 for _i in 0..n {
35 let mut r0 = 58.66;
36 let mut r = 0.7705 * r0 * segment;
37 for _j in 0..(n + 1) / 3 {
38 c.circle(0., r0, r)?;
39 let r2 = 1.058 * r;
40 r0 = r0 + r + r2;
41 r = r2;
42 }
43 c.stroke()?;
44 c.concat(Matrix::rotate(segment))?;
45 }
46
47 Ok(())
48 })
49 .unwrap();
50 document.finish().unwrap();
51}
9fn main() {
10 let mut document = Pdf::create("text.pdf").unwrap();
11 document.set_title("Text example");
12 document
13 .render_page(300.0, 400.0, |c| {
14 c.set_stroke_color(Color::rgb(200, 200, 255))?;
15 c.rectangle(10.0, 10.0, 280.0, 380.0)?;
16 c.line(10.0, 300.0, 290.0, 300.0)?;
17 c.line(150.0, 10.0, 150.0, 390.0)?;
18 c.stroke()?;
19 let helvetica = BuiltinFont::Helvetica;
20 c.left_text(10.0, 380.0, helvetica, 12.0, "Top left")?;
21 c.left_text(10.0, 10.0, helvetica, 12.0, "Bottom left")?;
22 c.right_text(290.0, 380.0, helvetica, 12.0, "Top right")?;
23 c.right_text(290.0, 10.0, helvetica, 12.0, "Bottom right")?;
24 c.center_text(
25 150.0,
26 330.0,
27 BuiltinFont::Times_Bold,
28 18.0,
29 "Centered",
30 )?;
31 let times = c.get_font(BuiltinFont::Times_Roman);
32 c.text(|t| {
33 t.set_font(×, 14.0)?;
34 t.set_leading(18.0)?;
35 t.pos(10.0, 300.0)?;
36 t.show("Some lines of text in what might look like a")?;
37 t.show_line("paragraph of three lines. Lorem ipsum dolor")?;
38 t.show_line("sit amet. Blahonga. ")?;
39 t.show_adjusted(&[("W", 130), ("AN", -40), ("D", 0)])?;
40 t.pos(0., -30.)?;
41 t.show_adjusted(
42 &(-19..21).map(|i| ("o", 16 * i)).collect::<Vec<_>>(),
43 )
44 })?;
45
46 // In Swedish, we use the letters å, ä, and ö
47 // in words like sloe liqueur. That is why rust-pdf
48 // uses /WinAnsiEncoding for text.
49 let times_italic = BuiltinFont::Times_Italic;
50 c.right_text(
51 290.0,
52 200.0,
53 times_italic,
54 14.0,
55 "På svenska använder vi bokstäverna å, ä & ö",
56 )?;
57 c.right_text(
58 290.0,
59 182.0,
60 times_italic,
61 14.0,
62 "i ord som slånbärslikör. Därför använder",
63 )?;
64 c.right_text(
65 290.0,
66 164.0,
67 times_italic,
68 14.0,
69 "rust-pdf /WinAnsiEncoding för text.",
70 )?;
71
72 c.center_text(
73 150.0,
74 130.0,
75 BuiltinFont::Symbol,
76 14.0,
77 "Hellas ΑΒΓΔαβγδ",
78 )?;
79 c.center_text(
80 150.0,
81 114.0,
82 BuiltinFont::Symbol,
83 14.0,
84 "∀ μ < δ : ∃ σ ∈ Σ",
85 )?;
86 c.center_text(
87 150.0,
88 90.0,
89 BuiltinFont::ZapfDingbats,
90 14.0,
91 "♥♠♦♣",
92 )?;
93 Ok(())
94 })
95 .unwrap();
96 document.finish().unwrap();
97}
Sourcepub fn set_title(&mut self, title: &str)
pub fn set_title(&mut self, title: &str)
Set metadata: the document’s title.
Examples found in repository?
9fn main() {
10 let mut document = Pdf::create("text.pdf").unwrap();
11 document.set_title("Text example");
12 document
13 .render_page(300.0, 400.0, |c| {
14 c.set_stroke_color(Color::rgb(200, 200, 255))?;
15 c.rectangle(10.0, 10.0, 280.0, 380.0)?;
16 c.line(10.0, 300.0, 290.0, 300.0)?;
17 c.line(150.0, 10.0, 150.0, 390.0)?;
18 c.stroke()?;
19 let helvetica = BuiltinFont::Helvetica;
20 c.left_text(10.0, 380.0, helvetica, 12.0, "Top left")?;
21 c.left_text(10.0, 10.0, helvetica, 12.0, "Bottom left")?;
22 c.right_text(290.0, 380.0, helvetica, 12.0, "Top right")?;
23 c.right_text(290.0, 10.0, helvetica, 12.0, "Bottom right")?;
24 c.center_text(
25 150.0,
26 330.0,
27 BuiltinFont::Times_Bold,
28 18.0,
29 "Centered",
30 )?;
31 let times = c.get_font(BuiltinFont::Times_Roman);
32 c.text(|t| {
33 t.set_font(×, 14.0)?;
34 t.set_leading(18.0)?;
35 t.pos(10.0, 300.0)?;
36 t.show("Some lines of text in what might look like a")?;
37 t.show_line("paragraph of three lines. Lorem ipsum dolor")?;
38 t.show_line("sit amet. Blahonga. ")?;
39 t.show_adjusted(&[("W", 130), ("AN", -40), ("D", 0)])?;
40 t.pos(0., -30.)?;
41 t.show_adjusted(
42 &(-19..21).map(|i| ("o", 16 * i)).collect::<Vec<_>>(),
43 )
44 })?;
45
46 // In Swedish, we use the letters å, ä, and ö
47 // in words like sloe liqueur. That is why rust-pdf
48 // uses /WinAnsiEncoding for text.
49 let times_italic = BuiltinFont::Times_Italic;
50 c.right_text(
51 290.0,
52 200.0,
53 times_italic,
54 14.0,
55 "På svenska använder vi bokstäverna å, ä & ö",
56 )?;
57 c.right_text(
58 290.0,
59 182.0,
60 times_italic,
61 14.0,
62 "i ord som slånbärslikör. Därför använder",
63 )?;
64 c.right_text(
65 290.0,
66 164.0,
67 times_italic,
68 14.0,
69 "rust-pdf /WinAnsiEncoding för text.",
70 )?;
71
72 c.center_text(
73 150.0,
74 130.0,
75 BuiltinFont::Symbol,
76 14.0,
77 "Hellas ΑΒΓΔαβγδ",
78 )?;
79 c.center_text(
80 150.0,
81 114.0,
82 BuiltinFont::Symbol,
83 14.0,
84 "∀ μ < δ : ∃ σ ∈ Σ",
85 )?;
86 c.center_text(
87 150.0,
88 90.0,
89 BuiltinFont::ZapfDingbats,
90 14.0,
91 "♥♠♦♣",
92 )?;
93 Ok(())
94 })
95 .unwrap();
96 document.finish().unwrap();
97}
Set metadata: the name of the person who created the document.
Sourcepub fn set_subject(&mut self, subject: &str)
pub fn set_subject(&mut self, subject: &str)
Set metadata: the subject of the document.
Sourcepub fn set_keywords(&mut self, keywords: &str)
pub fn set_keywords(&mut self, keywords: &str)
Set metadata: keywords associated with the document.
Sourcepub fn set_creator(&mut self, creator: &str)
pub fn set_creator(&mut self, creator: &str)
Set metadata: If the document was converted to PDF from another format, the name of the conforming product that created the original document from which it was converted.
Sourcepub fn set_producer(&mut self, producer: &str)
pub fn set_producer(&mut self, producer: &str)
Set metadata: If the document was converted to PDF from another format, the name of the conforming product that converted it to PDF.
Sourcepub fn render_page<F>(
&mut self,
width: f32,
height: f32,
render_contents: F,
) -> Result<()>
pub fn render_page<F>( &mut self, width: f32, height: f32, render_contents: F, ) -> Result<()>
Create a new page in the PDF document.
The page will be width
x height
points large, and the
actual content of the page will be created by the function
render_contents
by applying drawing methods on the Canvas.
Examples found in repository?
14fn main() {
15 // Open our pdf document.
16 let mut document = Pdf::create("circles.pdf").unwrap();
17
18 // Add a 400x400 pt page.
19
20 // Render-page writes the pdf file structure for a page and
21 // creates a Canvas which is sent to the function that is the last
22 // argument of the render_page method.
23 // That function then puts content on the page by calling methods
24 // on the canvas.
25 document
26 .render_page(400.0, 400.0, |c| {
27 let (x, y) = (200.0, 200.0);
28 let r = 190.0;
29
30 // Set a wide black pen and stroke a circle
31 c.set_stroke_color(Color::rgb(0, 0, 0))?;
32 c.set_line_width(2.0)?;
33 c.circle(x, y, r)?;
34 c.stroke()?;
35
36 // Set a finer yellow pen and stroke a 200-sided polygon
37 c.set_stroke_color(Color::rgb(255, 230, 150))?;
38 c.set_line_width(1.0)?;
39 c.move_to(x + r, y)?;
40 let sides: u8 = 200;
41 for n in 1..sides {
42 let phi = f32::from(n) * 2.0 * PI / f32::from(sides);
43 c.line_to(x + r * phi.cos(), y + r * phi.sin())?;
44 }
45 c.close_and_stroke()
46 })
47 .unwrap();
48 document.finish().unwrap();
49}
More examples
10fn main() {
11 // Open our pdf document.
12 let mut document = Pdf::create("mandala.pdf").expect("Create PDF file");
13 let mut args = env::args().skip(1);
14 let n: u8 = args.next().map(|s| s.parse().expect("number")).unwrap_or(7);
15
16 // Render a page with something resembling a mandala on it.
17 document
18 .render_page(600.0, 600.0, |c| {
19 c.concat(Matrix::translate(300., 300.))?;
20 c.set_stroke_color(Color::gray(0))?;
21 let segment = 2. * PI / n as f32;
22 for _i in 0..n {
23 c.move_to(0., 33.5)?;
24 c.line_to(0., 250.)?;
25 let r = 99.;
26 c.circle(0., r, r * 1.25 * segment)?;
27 let d = 141.4;
28 let rr = 36.;
29 c.circle(0., d + rr, rr)?;
30 c.stroke()?;
31 c.concat(Matrix::rotate(segment))?;
32 }
33 c.concat(Matrix::rotate(segment / 2.))?;
34 for _i in 0..n {
35 let mut r0 = 58.66;
36 let mut r = 0.7705 * r0 * segment;
37 for _j in 0..(n + 1) / 3 {
38 c.circle(0., r0, r)?;
39 let r2 = 1.058 * r;
40 r0 = r0 + r + r2;
41 r = r2;
42 }
43 c.stroke()?;
44 c.concat(Matrix::rotate(segment))?;
45 }
46
47 Ok(())
48 })
49 .unwrap();
50 document.finish().unwrap();
51}
9fn main() {
10 let mut document = Pdf::create("text.pdf").unwrap();
11 document.set_title("Text example");
12 document
13 .render_page(300.0, 400.0, |c| {
14 c.set_stroke_color(Color::rgb(200, 200, 255))?;
15 c.rectangle(10.0, 10.0, 280.0, 380.0)?;
16 c.line(10.0, 300.0, 290.0, 300.0)?;
17 c.line(150.0, 10.0, 150.0, 390.0)?;
18 c.stroke()?;
19 let helvetica = BuiltinFont::Helvetica;
20 c.left_text(10.0, 380.0, helvetica, 12.0, "Top left")?;
21 c.left_text(10.0, 10.0, helvetica, 12.0, "Bottom left")?;
22 c.right_text(290.0, 380.0, helvetica, 12.0, "Top right")?;
23 c.right_text(290.0, 10.0, helvetica, 12.0, "Bottom right")?;
24 c.center_text(
25 150.0,
26 330.0,
27 BuiltinFont::Times_Bold,
28 18.0,
29 "Centered",
30 )?;
31 let times = c.get_font(BuiltinFont::Times_Roman);
32 c.text(|t| {
33 t.set_font(×, 14.0)?;
34 t.set_leading(18.0)?;
35 t.pos(10.0, 300.0)?;
36 t.show("Some lines of text in what might look like a")?;
37 t.show_line("paragraph of three lines. Lorem ipsum dolor")?;
38 t.show_line("sit amet. Blahonga. ")?;
39 t.show_adjusted(&[("W", 130), ("AN", -40), ("D", 0)])?;
40 t.pos(0., -30.)?;
41 t.show_adjusted(
42 &(-19..21).map(|i| ("o", 16 * i)).collect::<Vec<_>>(),
43 )
44 })?;
45
46 // In Swedish, we use the letters å, ä, and ö
47 // in words like sloe liqueur. That is why rust-pdf
48 // uses /WinAnsiEncoding for text.
49 let times_italic = BuiltinFont::Times_Italic;
50 c.right_text(
51 290.0,
52 200.0,
53 times_italic,
54 14.0,
55 "På svenska använder vi bokstäverna å, ä & ö",
56 )?;
57 c.right_text(
58 290.0,
59 182.0,
60 times_italic,
61 14.0,
62 "i ord som slånbärslikör. Därför använder",
63 )?;
64 c.right_text(
65 290.0,
66 164.0,
67 times_italic,
68 14.0,
69 "rust-pdf /WinAnsiEncoding för text.",
70 )?;
71
72 c.center_text(
73 150.0,
74 130.0,
75 BuiltinFont::Symbol,
76 14.0,
77 "Hellas ΑΒΓΔαβγδ",
78 )?;
79 c.center_text(
80 150.0,
81 114.0,
82 BuiltinFont::Symbol,
83 14.0,
84 "∀ μ < δ : ∃ σ ∈ Σ",
85 )?;
86 c.center_text(
87 150.0,
88 90.0,
89 BuiltinFont::ZapfDingbats,
90 14.0,
91 "♥♠♦♣",
92 )?;
93 Ok(())
94 })
95 .unwrap();
96 document.finish().unwrap();
97}
Sourcepub fn finish(self) -> Result<()>
pub fn finish(self) -> Result<()>
Write out the document trailer. The trailer consists of the pages object, the root object, the xref list, the trailer object and the startxref position.
Examples found in repository?
14fn main() {
15 // Open our pdf document.
16 let mut document = Pdf::create("circles.pdf").unwrap();
17
18 // Add a 400x400 pt page.
19
20 // Render-page writes the pdf file structure for a page and
21 // creates a Canvas which is sent to the function that is the last
22 // argument of the render_page method.
23 // That function then puts content on the page by calling methods
24 // on the canvas.
25 document
26 .render_page(400.0, 400.0, |c| {
27 let (x, y) = (200.0, 200.0);
28 let r = 190.0;
29
30 // Set a wide black pen and stroke a circle
31 c.set_stroke_color(Color::rgb(0, 0, 0))?;
32 c.set_line_width(2.0)?;
33 c.circle(x, y, r)?;
34 c.stroke()?;
35
36 // Set a finer yellow pen and stroke a 200-sided polygon
37 c.set_stroke_color(Color::rgb(255, 230, 150))?;
38 c.set_line_width(1.0)?;
39 c.move_to(x + r, y)?;
40 let sides: u8 = 200;
41 for n in 1..sides {
42 let phi = f32::from(n) * 2.0 * PI / f32::from(sides);
43 c.line_to(x + r * phi.cos(), y + r * phi.sin())?;
44 }
45 c.close_and_stroke()
46 })
47 .unwrap();
48 document.finish().unwrap();
49}
More examples
10fn main() {
11 // Open our pdf document.
12 let mut document = Pdf::create("mandala.pdf").expect("Create PDF file");
13 let mut args = env::args().skip(1);
14 let n: u8 = args.next().map(|s| s.parse().expect("number")).unwrap_or(7);
15
16 // Render a page with something resembling a mandala on it.
17 document
18 .render_page(600.0, 600.0, |c| {
19 c.concat(Matrix::translate(300., 300.))?;
20 c.set_stroke_color(Color::gray(0))?;
21 let segment = 2. * PI / n as f32;
22 for _i in 0..n {
23 c.move_to(0., 33.5)?;
24 c.line_to(0., 250.)?;
25 let r = 99.;
26 c.circle(0., r, r * 1.25 * segment)?;
27 let d = 141.4;
28 let rr = 36.;
29 c.circle(0., d + rr, rr)?;
30 c.stroke()?;
31 c.concat(Matrix::rotate(segment))?;
32 }
33 c.concat(Matrix::rotate(segment / 2.))?;
34 for _i in 0..n {
35 let mut r0 = 58.66;
36 let mut r = 0.7705 * r0 * segment;
37 for _j in 0..(n + 1) / 3 {
38 c.circle(0., r0, r)?;
39 let r2 = 1.058 * r;
40 r0 = r0 + r + r2;
41 r = r2;
42 }
43 c.stroke()?;
44 c.concat(Matrix::rotate(segment))?;
45 }
46
47 Ok(())
48 })
49 .unwrap();
50 document.finish().unwrap();
51}
9fn main() {
10 let mut document = Pdf::create("text.pdf").unwrap();
11 document.set_title("Text example");
12 document
13 .render_page(300.0, 400.0, |c| {
14 c.set_stroke_color(Color::rgb(200, 200, 255))?;
15 c.rectangle(10.0, 10.0, 280.0, 380.0)?;
16 c.line(10.0, 300.0, 290.0, 300.0)?;
17 c.line(150.0, 10.0, 150.0, 390.0)?;
18 c.stroke()?;
19 let helvetica = BuiltinFont::Helvetica;
20 c.left_text(10.0, 380.0, helvetica, 12.0, "Top left")?;
21 c.left_text(10.0, 10.0, helvetica, 12.0, "Bottom left")?;
22 c.right_text(290.0, 380.0, helvetica, 12.0, "Top right")?;
23 c.right_text(290.0, 10.0, helvetica, 12.0, "Bottom right")?;
24 c.center_text(
25 150.0,
26 330.0,
27 BuiltinFont::Times_Bold,
28 18.0,
29 "Centered",
30 )?;
31 let times = c.get_font(BuiltinFont::Times_Roman);
32 c.text(|t| {
33 t.set_font(×, 14.0)?;
34 t.set_leading(18.0)?;
35 t.pos(10.0, 300.0)?;
36 t.show("Some lines of text in what might look like a")?;
37 t.show_line("paragraph of three lines. Lorem ipsum dolor")?;
38 t.show_line("sit amet. Blahonga. ")?;
39 t.show_adjusted(&[("W", 130), ("AN", -40), ("D", 0)])?;
40 t.pos(0., -30.)?;
41 t.show_adjusted(
42 &(-19..21).map(|i| ("o", 16 * i)).collect::<Vec<_>>(),
43 )
44 })?;
45
46 // In Swedish, we use the letters å, ä, and ö
47 // in words like sloe liqueur. That is why rust-pdf
48 // uses /WinAnsiEncoding for text.
49 let times_italic = BuiltinFont::Times_Italic;
50 c.right_text(
51 290.0,
52 200.0,
53 times_italic,
54 14.0,
55 "På svenska använder vi bokstäverna å, ä & ö",
56 )?;
57 c.right_text(
58 290.0,
59 182.0,
60 times_italic,
61 14.0,
62 "i ord som slånbärslikör. Därför använder",
63 )?;
64 c.right_text(
65 290.0,
66 164.0,
67 times_italic,
68 14.0,
69 "rust-pdf /WinAnsiEncoding för text.",
70 )?;
71
72 c.center_text(
73 150.0,
74 130.0,
75 BuiltinFont::Symbol,
76 14.0,
77 "Hellas ΑΒΓΔαβγδ",
78 )?;
79 c.center_text(
80 150.0,
81 114.0,
82 BuiltinFont::Symbol,
83 14.0,
84 "∀ μ < δ : ∃ σ ∈ Σ",
85 )?;
86 c.center_text(
87 150.0,
88 90.0,
89 BuiltinFont::ZapfDingbats,
90 14.0,
91 "♥♠♦♣",
92 )?;
93 Ok(())
94 })
95 .unwrap();
96 document.finish().unwrap();
97}