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 anyhow::{anyhow, Context, Error};
|
||||||
use bingus::{
|
use bingus::{
|
||||||
doc::printpdf::{FontId, Op, PdfDocument, TextItem},
|
doc::printpdf::PdfDocument,
|
||||||
Bendable,
|
img::{Dimensions, GrayImage, RgbImage},
|
||||||
|
Bendable, Bytes,
|
||||||
};
|
};
|
||||||
use clap::Parser;
|
use clap::Parser;
|
||||||
use cli::Cli;
|
use cli::Cli;
|
||||||
|
@ -12,15 +13,22 @@ mod cli;
|
||||||
|
|
||||||
fn main() -> Result<(), Error> {
|
fn main() -> Result<(), Error> {
|
||||||
let args = Cli::parse();
|
let args = Cli::parse();
|
||||||
File::open(args.input_file)
|
let bytes = File::open(args.input_file)
|
||||||
.context("opening file")?
|
.context("opening file")?
|
||||||
.bend_into::<PdfDocument>((), Default::default())
|
.bend_into::<PdfDocument>((), Default::default())
|
||||||
.map_err(|e| anyhow!("parsing PDF: {e}"))?
|
.map_err(|e| anyhow!("parsing PDF: {e}"))?
|
||||||
.map(|t| Op::WriteText {
|
.bend_into::<Bytes>((), Default::default())?;
|
||||||
items: vec![TextItem::Text(String::from("h"))],
|
let dimensions = {
|
||||||
font: FontId::new(),
|
let c = bytes.len().isqrt().try_into().context("image too big")?;
|
||||||
})
|
Dimensions {
|
||||||
.bend_into::<File>(Box::new(args.output_file), Default::default())
|
width: c,
|
||||||
|
height: c,
|
||||||
|
}
|
||||||
|
};
|
||||||
|
bytes
|
||||||
|
.bend_into::<GrayImage>(dimensions, Default::default())
|
||||||
|
.context("bending into an image")?
|
||||||
|
.save(args.output_file)
|
||||||
.context("saving output file")?;
|
.context("saving output file")?;
|
||||||
|
|
||||||
Ok(())
|
Ok(())
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue