diff --git a/crates/typst-cli/src/args.rs b/crates/typst-cli/src/args.rs index 3f19962c48f0..420c5467099c 100644 --- a/crates/typst-cli/src/args.rs +++ b/crates/typst-cli/src/args.rs @@ -67,6 +67,12 @@ pub struct CompileCommand { /// Path to output file (PDF, PNG, or SVG) pub output: Option, + #[clap( + long = "stdout", + default_value_t = false, + )] + pub output_to_stdout: bool, + /// The format of the output file, inferred from the extension by default #[arg(long = "format", short = 'f')] pub format: Option, diff --git a/crates/typst-cli/src/compile.rs b/crates/typst-cli/src/compile.rs index ea612630801f..1aaefc86fb39 100644 --- a/crates/typst-cli/src/compile.rs +++ b/crates/typst-cli/src/compile.rs @@ -1,4 +1,5 @@ use std::fs; +use std::io::Write; use std::path::{Path, PathBuf}; use chrono::{Datelike, Timelike}; @@ -166,9 +167,19 @@ fn export_pdf( ) -> StrResult<()> { let ident = world.input().to_string_lossy(); let buffer = typst_pdf::pdf(document, Some(&ident), now()); - let output = command.output(); - fs::write(output, buffer) - .map_err(|err| eco_format!("failed to write PDF file ({err})"))?; + + // If to stdout + if command.output_to_stdout { + let mut stdout = std::io::stdout().lock(); + stdout.write_all(&buffer) + .map_err(|err| eco_format!("failed to write PDF to stdout ({err})"))?; + } + // If not to stdout or output file is explicitly set + if !command.output_to_stdout | command.output.is_some() { + let output = command.output(); + fs::write(output, buffer) + .map_err(|err| eco_format!("failed to write PDF file ({err})"))?; + } Ok(()) }