1
0
Fork 0
mirror of https://github.com/kou029w/_.git synced 2025-01-30 22:08:02 +00:00

create poppler-rs

This commit is contained in:
Nebel 2024-09-01 23:12:17 +09:00
parent 84b0494547
commit 2794034e55
Signed by: nebel
GPG key ID: 79807D08C6EF6460
4 changed files with 1640 additions and 0 deletions

1
poppler-rs/.gitignore vendored Normal file
View file

@ -0,0 +1 @@
/target

1573
poppler-rs/Cargo.lock generated Normal file

File diff suppressed because it is too large Load diff

11
poppler-rs/Cargo.toml Normal file
View file

@ -0,0 +1,11 @@
[package]
edition = "2021"
name = "poppler-rs"
version = "0.1.0"
[dependencies]
cairo-rs = {version = "0.20.1"}
clap = {version = "4.5.16", features = ["derive", "env"]}
image = "0.25.2"
poppler-rs = "0.24.1"
url = "2.5.2"

55
poppler-rs/src/main.rs Normal file
View file

@ -0,0 +1,55 @@
use cairo::{Context, Format, ImageSurface};
use clap::Parser;
use image::codecs::webp::WebPEncoder;
use image::ExtendedColorType;
use poppler::Document;
use std::fs;
use std::fs::File;
use url::Url;
#[derive(Parser, Debug)]
#[command(version)]
struct Args {
#[arg(short, long, env, default_value = "target/note/note.pdf", value_parser = canonicalize)]
input: String,
#[arg(short, long, env, default_value = "target/output")]
output: String,
}
fn canonicalize(arg: &str) -> Result<String, std::io::Error> {
let path = fs::canonicalize(arg)?.to_string_lossy().into_owned();
Ok(path)
}
fn main() {
let args = Args::parse();
let input = args.input;
let output = args.output;
let input_uri = Url::parse(format!("file://{input}").as_str())
.unwrap()
.to_string();
let document = Document::from_file(&input_uri, None).unwrap();
for i in 0..document.n_pages() {
let page = document.page(i).unwrap();
let (width, height) = page.size();
let output_file = File::create(format!("{output}-{i}.webp")).unwrap();
let surface = ImageSurface::create(Format::Rgb24, width as i32, height as i32).unwrap();
let context = Context::new(&surface).unwrap();
context.set_source_rgb(1., 1., 1.);
context.paint().unwrap();
page.render(&context);
drop(context);
let data = surface.take_data().unwrap();
let encoder = WebPEncoder::new_lossless(output_file);
encoder
.encode(&data, width as u32, height as u32, ExtendedColorType::Rgba8)
.unwrap();
}
}