Skip to content

Commit

Permalink
Add page orientation option
Browse files Browse the repository at this point in the history
  • Loading branch information
MaeIsBad committed Sep 24, 2024
1 parent af2bda2 commit 4535ff3
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 7 deletions.
23 changes: 21 additions & 2 deletions src/args.rs
Original file line number Diff line number Diff line change
@@ -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",
Expand All @@ -26,6 +27,24 @@ pub struct Args {
/// support. You are responsible for sanitizing them to prevent these issues
pub input_url: Option<Url>,

#[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<PageOrientation> for Orientation {
fn into(self) -> PageOrientation {
match self {
Self::Portrait => PageOrientation::Portrait,
Self::Landscape => PageOrientation::Landscape
}
}
}
2 changes: 1 addition & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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?;

Expand Down
14 changes: 10 additions & 4 deletions src/printer.rs
Original file line number Diff line number Diff line change
@@ -1,29 +1,35 @@
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<()> {
let file = std::path::absolute(self.output_file)?;
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();
Expand Down

0 comments on commit 4535ff3

Please sign in to comment.