use bookify_rs::{
args::{BaseOptions, BookletOptions, DoubleSidedOptions, FlipType, LayoutType, OddEven},
imposition::PdfImposer,
};
use std::fs;
use std::path::PathBuf;
const DELETE_RESULT: bool = false;
#[test]
fn test_booklet_imposition() {
let input_path = PathBuf::from("tests/sample.pdf");
let output_path = PathBuf::from("tests/output/booklet-test.pdf");
fs::create_dir_all("tests/output").unwrap();
let opts = BookletOptions {
base: BaseOptions {
input: input_path.clone(),
output: Some(output_path.clone()),
temp: false,
},
layout: LayoutType::TwoUp,
};
let mut imposer = PdfImposer::new(input_path).unwrap();
imposer.export_booklet(opts.layout).unwrap();
imposer.save(output_path.clone()).unwrap();
assert!(output_path.exists());
if DELETE_RESULT {
fs::remove_file(output_path).unwrap();
}
}
#[test]
fn test_double_sided_imposition_odd() {
let input_path = PathBuf::from("tests/sample.pdf");
let output_path = PathBuf::from("tests/output/double-sided-test-odd.pdf");
fs::create_dir_all("tests/output").unwrap();
let opts = DoubleSidedOptions {
base: BaseOptions {
input: input_path.clone(),
output: Some(output_path.clone()),
temp: false,
},
flip_type: FlipType::RR,
odd_even: OddEven::Odd,
};
let mut imposer = PdfImposer::new(input_path.clone()).unwrap();
imposer
.export_double_sided(opts.flip_type, opts.odd_even)
.unwrap();
imposer.save(output_path.clone()).unwrap();
assert!(output_path.exists());
if DELETE_RESULT {
fs::remove_file(output_path).unwrap();
}
}
#[test]
fn test_double_sided_imposition_even() {
let input_path = PathBuf::from("tests/sample.pdf");
let output_path = PathBuf::from("tests/output/double-sided-test-even.pdf");
fs::create_dir_all("tests/output").unwrap();
let opts = DoubleSidedOptions {
base: BaseOptions {
input: input_path.clone(),
output: Some(output_path.clone()),
temp: false,
},
flip_type: FlipType::RR,
odd_even: OddEven::Even,
};
let mut imposer = PdfImposer::new(input_path).unwrap();
imposer
.export_double_sided(opts.flip_type, opts.odd_even)
.unwrap();
imposer.save(output_path.clone()).unwrap();
assert!(output_path.exists());
if DELETE_RESULT {
fs::remove_file(output_path).unwrap();
}
}
#[test]
fn test_temp_output() {
let input_path = PathBuf::from("tests/sample.pdf");
let opts = BookletOptions {
base: BaseOptions {
input: input_path.clone(),
output: None,
temp: true,
},
layout: LayoutType::TwoUp,
};
let mut imposer = PdfImposer::new(input_path).unwrap();
imposer.export_booklet(opts.layout).unwrap();
let temp_file = tempfile::Builder::new()
.prefix("booklet-")
.suffix(".pdf")
.tempfile()
.unwrap();
let temp_path = temp_file.path().to_path_buf();
imposer.save(temp_path.clone()).unwrap();
assert!(temp_path.exists());
}
#[test]
fn test_custom_output_path() {
let input_path = PathBuf::from("tests/sample.pdf");
let custom_output = PathBuf::from("tests/output/custom-test.pdf");
fs::create_dir_all("tests/output").unwrap();
let opts = BookletOptions {
base: BaseOptions {
input: input_path.clone(),
output: Some(custom_output.clone()),
temp: false,
},
layout: LayoutType::TwoUp,
};
let mut imposer = PdfImposer::new(input_path).unwrap();
imposer.export_booklet(opts.layout).unwrap();
imposer.save(custom_output.clone()).unwrap();
assert!(custom_output.exists());
if DELETE_RESULT {
fs::remove_file(custom_output).unwrap();
}
}