diff --git a/src/args.rs b/src/args.rs index 84a9e0d..85b1b1f 100644 --- a/src/args.rs +++ b/src/args.rs @@ -1,8 +1,9 @@ -use clap::Parser; +use clap::{ValueEnum, Parser}; +use gtk4::PageOrientation; use std::path::PathBuf; use url::Url; -#[derive(Clone, Parser)] +#[derive(Debug, Clone, Parser)] pub struct Args { #[arg( name = "file", @@ -26,6 +27,24 @@ pub struct Args { /// support. You are responsible for sanitizing them to prevent these issues pub input_url: Option, + #[arg(long, default_value = "portrait")] + pub orientation: Orientation, + #[arg(default_value = "output.pdf")] pub output_file: PathBuf, } + +#[derive(ValueEnum, Debug, Clone, Parser)] +pub enum Orientation { + Portrait, + Landscape +} + +impl Into for Orientation { + fn into(self) -> PageOrientation { + match self { + Self::Portrait => PageOrientation::Portrait, + Self::Landscape => PageOrientation::Landscape + } + } +} diff --git a/src/lib.rs b/src/lib.rs index 8fd7b91..6015591 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -26,7 +26,7 @@ async fn do_print(args: Args, window: ApplicationWindow) -> Result<()> { }; let webview = webview_cfg.run(&window).await?; - printer::PrintConfig::new(args.output_file.clone()) + printer::PrintConfig::new(args.output_file.clone(), args.orientation) .print(&webview) .await?; diff --git a/src/printer.rs b/src/printer.rs index a680678..eb0e6c9 100644 --- a/src/printer.rs +++ b/src/printer.rs @@ -1,18 +1,19 @@ use anyhow::{Context, Result}; use glib_macros::clone; -use gtk4::{prelude::ObjectExt, PrintSettings}; +use gtk4::{prelude::ObjectExt, PageSetup, PrintSettings}; use std::path::PathBuf; use url::Url; use webkit6::{PrintOperation, WebView}; -use crate::utils; +use crate::{args::Orientation, utils}; pub struct PrintConfig { + orientation: Orientation, output_file: PathBuf, } impl PrintConfig { - pub fn new(output_file: PathBuf) -> Self { - Self { output_file } + pub fn new(output_file: PathBuf, orientation: Orientation) -> Self { + Self { output_file, orientation } } pub async fn print(self, webview: &WebView) -> Result<()> { @@ -20,10 +21,15 @@ impl PrintConfig { let output_uri = Url::from_file_path(file).unwrap(); let print_op = PrintOperation::new(webview); + let page_setup = PageSetup::new(); + page_setup.set_orientation(self.orientation.into()); + let settings = PrintSettings::new(); settings.set_printer("Print to File"); settings.set(gtk4::PRINT_SETTINGS_OUTPUT_URI, Some(output_uri.as_str())); + + print_op.set_page_setup(&page_setup); print_op.set_print_settings(&settings); let (s, r) = utils::runtime_oneshot();