Skip to content

Commit

Permalink
Merge pull request #10 from randommm/main
Browse files Browse the repository at this point in the history
Add disable-sandbox option
  • Loading branch information
ilaborie committed Oct 17, 2023
2 parents 540024d + 81fc6af commit d4fa912
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 25 deletions.
38 changes: 20 additions & 18 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,26 +37,28 @@ USAGE:
html2pdf [FLAGS] [OPTIONS] <input>

FLAGS:
--background Allow print background
-h, --help Prints help information
--landscape Use landscape mode
-V, --version Prints version information
--background Allow print background
-h, --help Prints help information
--landscape Use landscape mode
--disable-sandbox Disable Chrome sandbox. Not recommended, unless running on docker

-V, --version Prints version information

OPTIONS:
--footer <footer> HTML template for the print footer. Should use the same format as the headerTemplate
--header <header> HTML template for the print header. Should be valid HTML markup with following classes used
to inject printing values into them: date for formatted print date, title for document
title, url for document location, pageNumber for current page number, totalPages for total
pages in the document. For example, `<span class=title></span>` would generate span
containing the title
--margin <margin> Margin in inches You can define margin like this: '0.4' the value is applied for all side,
'0.4 0.4' : first value is applied for top and bottom, second for left and right, '0.4 0.4
0.4 0.4' : first value is applied for top then, right, then bottom, and last for left
-o, --output <output> Output file. By default, just change the input extension to PDF
--paper <paper> Paper size. Supported values: A4, Letter, A3, Tabloid, A2, A1, A0, A5, A6
--range <range> Paper ranges to print, e.g. '1-5, 8, 11-13'
--scale <scale> Scale, default to 1.0
--wait <wait> Time to wait in ms before printing. Examples: 150ms, 10s
--footer <footer> HTML template for the print footer. Should use the same format as the headerTemplate
--header <header> HTML template for the print header. Should be valid HTML markup with following classes used
to inject printing values into them: date for formatted print date, title for document
title, url for document location, pageNumber for current page number, totalPages for total
pages in the document. For example, `<span class=title></span>` would generate span
containing the title
--margin <margin> Margin in inches You can define margin like this: '0.4' the value is applied for all side,
'0.4 0.4' : first value is applied for top and bottom, second for left and right, '0.4 0.4
0.4 0.4' : first value is applied for top then, right, then bottom, and last for left
-o, --output <output> Output file. By default, just change the input extension to PDF
--paper <paper> Paper size. Supported values: A4, Letter, A3, Tabloid, A2, A1, A0, A5, A6
--range <range> Paper ranges to print, e.g. '1-5, 8, 11-13'
--scale <scale> Scale, default to 1.0
--wait <wait> Time to wait in ms before printing. Examples: 150ms, 10s

ARGS:
<input> Input HTML file
Expand Down
21 changes: 21 additions & 0 deletions src/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ use std::time::Duration;

use clap::Parser;
use headless_chrome::types::PrintToPdfOptions;
use headless_chrome::LaunchOptions;
use humantime::parse_duration;

use crate::Error;
Expand Down Expand Up @@ -70,6 +71,11 @@ pub struct Options {
/// '0.4 0.4 0.4 0.4' : first value is applied for top then, right, then bottom, and last for left
#[clap(long)]
pub margin: Option<Margin>,

/// Disable Chrome sandbox
/// Not recommended, unless running on docker
#[clap(long)]
pub disable_sandbox: bool,
}

impl Options {
Expand Down Expand Up @@ -138,6 +144,12 @@ impl Options {
pub fn range(&self) -> Option<&String> {
self.range.as_ref()
}

/// Get a reference to the cli options's sandbox.
#[must_use]
pub fn disable_sandbox(&self) -> bool {
self.disable_sandbox
}
}

impl From<&Options> for PrintToPdfOptions {
Expand All @@ -163,6 +175,15 @@ impl From<&Options> for PrintToPdfOptions {
}
}

impl From<&Options> for LaunchOptions<'_> {
fn from(opt: &Options) -> Self {
LaunchOptions {
sandbox: !opt.disable_sandbox(),
..Default::default()
}
}
}

/// Paper size
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum PaperSize {
Expand Down
13 changes: 6 additions & 7 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ use std::{fs, io};

use anyhow::Result;
use headless_chrome::types::PrintToPdfOptions;
use headless_chrome::{Browser, LaunchOptionsBuilder};
use headless_chrome::{Browser, LaunchOptions};
use humantime::format_duration;
use log::{debug, info};
use thiserror::Error;
Expand Down Expand Up @@ -78,7 +78,7 @@ pub fn run(opt: &Options) -> Result<(), Error> {
path
};

html_to_pdf(input, output, opt.into(), opt.wait())?;
html_to_pdf(input, output, opt.into(), opt.into(), opt.wait())?;

Ok(())
}
Expand All @@ -95,6 +95,7 @@ pub fn html_to_pdf<I, O>(
input: I,
output: O,
pdf_options: PrintToPdfOptions,
launch_options: LaunchOptions,
wait: Option<Duration>,
) -> Result<(), Error>
where
Expand All @@ -109,7 +110,7 @@ where
let input = format!("file://{os}");
info!("Input file: {input}");

let local_pdf = print_to_pdf(&input, pdf_options, wait)?;
let local_pdf = print_to_pdf(&input, pdf_options, launch_options, wait)?;

info!("Output file: {:?}", output.as_ref());
fs::write(output.as_ref(), local_pdf)?;
Expand All @@ -120,12 +121,10 @@ where
fn print_to_pdf(
file_path: &str,
pdf_options: PrintToPdfOptions,
launch_options: LaunchOptions,
wait: Option<Duration>,
) -> Result<Vec<u8>> {
let options = LaunchOptionsBuilder::default()
.build()
.expect("Default should not panic");
let browser = Browser::new(options)?;
let browser = Browser::new(launch_options)?;
let tab = browser.new_tab()?;
let tab = tab.navigate_to(file_path)?.wait_until_navigated()?;

Expand Down

0 comments on commit d4fa912

Please sign in to comment.