-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add initial pdf support (#114)
* enable pdf output format for the command line * add working pdf export * remove the unnecessary keeping of the temp html file * use correct error kind * move pdf render code to own module in the render crate * first working import of paged js * rebase follow up fixes * implemented get_target function for the UmiRenderer * fix the missing code highlighting in pdfs * improve error message output during the pdf render stage * fix: use recommended formatting for pdfs --------- Co-authored-by: Tobias Kleinert <[email protected]> Co-authored-by: Sowasvonbot <> Co-authored-by: Manuel Hatzl <[email protected]>
- Loading branch information
1 parent
b133fb6
commit 498ca1f
Showing
17 changed files
with
482 additions
and
16 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6,5 +6,6 @@ | |
mod csl_json; | ||
pub mod html; | ||
pub mod log_id; | ||
pub mod pdf; | ||
pub mod render; | ||
pub mod umi; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
pub mod render; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
use std::io::Write; | ||
|
||
use headless_chrome::types::PrintToPdfOptions; | ||
use headless_chrome::{Browser, LaunchOptions}; | ||
use tempfile::Builder; | ||
|
||
use crate::log_id::RenderError; | ||
use crate::log_id::RenderError::UnexpectedPdfError; | ||
|
||
/// Returns PrintToPdfOptions following the recommended settings of: | ||
/// https://pagedjs.org/documentation/2-getting-started-with-paged.js/#using-paged.js-as-a-polyfill-in-web-browsers | ||
fn create_pdf_options() -> Option<PrintToPdfOptions> { | ||
Some(PrintToPdfOptions { | ||
margin_top: Some(0f64), | ||
margin_bottom: Some(0f64), | ||
margin_left: Some(0f64), | ||
margin_right: Some(0f64), | ||
header_template: None, | ||
footer_template: None, | ||
print_background: Some(false), | ||
..PrintToPdfOptions::default() | ||
}) | ||
} | ||
|
||
/// Renders the given html-string to a pdf represent as bytes. | ||
/// It first writes the html-string to a temp-directory, because chrome needs a file to load as webpage. | ||
/// Then it prints the rendered html as pdf. The result is returned and not written to disc. | ||
/// | ||
/// # Arguments | ||
/// * `html` - The rendered html as string | ||
/// | ||
/// # Returns | ||
/// The rendered PDF as bytes. | ||
/// | ||
/// # Errors | ||
/// * `UnexpectedPdfError` - in case something goes wrong with the underlying headless-chrome framework. | ||
pub fn render_pdf(html: &str) -> Result<Vec<u8>, RenderError> { | ||
let mut temp_html_file = Builder::new() | ||
.suffix(".html") | ||
.tempfile() | ||
.map_err(|err| UnexpectedPdfError(err.to_string()))?; | ||
|
||
temp_html_file | ||
.write_all(html.as_bytes()) | ||
.map_err(|err| UnexpectedPdfError(err.to_string()))?; | ||
let temp_file_url = format!( | ||
"file://{}", | ||
temp_html_file | ||
.as_ref() | ||
.as_os_str() | ||
.to_str() | ||
.ok_or(RenderError::Unimplemented)? | ||
); | ||
|
||
let browser = Browser::new(LaunchOptions::default()) | ||
.map_err(|err| UnexpectedPdfError(err.to_string()))?; | ||
let pdf_bytes = browser | ||
.new_tab() | ||
.map_err(|err| UnexpectedPdfError(err.to_string()))? | ||
.navigate_to(temp_file_url.as_str()) | ||
.map_err(|err| UnexpectedPdfError(err.to_string()))? | ||
.wait_until_navigated() | ||
.map_err(|err| UnexpectedPdfError(err.to_string()))? | ||
.print_to_pdf(create_pdf_options()) | ||
.map_err(|err| UnexpectedPdfError(err.to_string()))?; | ||
|
||
Ok(pdf_bytes) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.