pdf to image example
This commit is contained in:
parent
dc74662263
commit
b00ce70504
1 changed files with 16 additions and 8 deletions
|
@ -2,8 +2,9 @@ use std::fs::File;
|
|||
|
||||
use anyhow::{anyhow, Context, Error};
|
||||
use bingus::{
|
||||
doc::printpdf::{FontId, Op, PdfDocument, TextItem},
|
||||
Bendable,
|
||||
doc::printpdf::PdfDocument,
|
||||
img::{Dimensions, GrayImage, RgbImage},
|
||||
Bendable, Bytes,
|
||||
};
|
||||
use clap::Parser;
|
||||
use cli::Cli;
|
||||
|
@ -12,15 +13,22 @@ mod cli;
|
|||
|
||||
fn main() -> Result<(), Error> {
|
||||
let args = Cli::parse();
|
||||
File::open(args.input_file)
|
||||
let bytes = File::open(args.input_file)
|
||||
.context("opening file")?
|
||||
.bend_into::<PdfDocument>((), Default::default())
|
||||
.map_err(|e| anyhow!("parsing PDF: {e}"))?
|
||||
.map(|t| Op::WriteText {
|
||||
items: vec![TextItem::Text(String::from("h"))],
|
||||
font: FontId::new(),
|
||||
})
|
||||
.bend_into::<File>(Box::new(args.output_file), Default::default())
|
||||
.bend_into::<Bytes>((), Default::default())?;
|
||||
let dimensions = {
|
||||
let c = bytes.len().isqrt().try_into().context("image too big")?;
|
||||
Dimensions {
|
||||
width: c,
|
||||
height: c,
|
||||
}
|
||||
};
|
||||
bytes
|
||||
.bend_into::<GrayImage>(dimensions, Default::default())
|
||||
.context("bending into an image")?
|
||||
.save(args.output_file)
|
||||
.context("saving output file")?;
|
||||
|
||||
Ok(())
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue