use easypdf::prelude::*;
use std::path::PathBuf;
fn main() -> Result<(), Box<dyn std::error::Error>> {
let out_dir = tempfile::tempdir()?;
let out_path: PathBuf = out_dir.path().join("multi_page.pdf");
let mut writer = EasyPdf::writer("Multi-Page Report")
.metadata(
PdfMetadata::new()
.author("easypdf examples")
.subject("Multi-page demonstration"),
)
.build()?;
let chapters = [
(
"Chapter 1: Introduction",
"Welcome to the multi-page example.",
),
(
"Chapter 2: Features",
"easypdf supports tables, images, and more.",
),
("Chapter 3: Conclusion", "Thank you for trying easypdf!"),
];
for (title, body) in &chapters {
writer.add_page(PageSize::A4, Orientation::Portrait)?;
let title_text = PdfText::new(*title)
.font(PdfFont::helvetica(20.0).bold())
.color(PdfColor::blue());
writer.write_text(&title_text, 72.0, 750.0)?;
let body_text = PdfText::new(*body).font(PdfFont::helvetica(12.0));
writer.write_text(&body_text, 72.0, 700.0)?;
let page_num = writer.page_count();
let page_text = PdfText::new(format!("- {page_num} -"))
.font(PdfFont::helvetica(10.0))
.alignment(TextAlignment::Center);
writer.write_text(&page_text, 297.5, 30.0)?;
}
writer.finish(&out_path)?;
println!("Created multi-page PDF at: {}", out_path.display());
let count = EasyPdf::read(&out_path).page_count()?;
println!("Total pages: {count}");
Ok(())
}