tinymist_task/compute/
pdf.rs

1use tinymist_std::time::ToUtcDateTime;
2pub use typst_pdf::pdf;
3pub use typst_pdf::PdfStandard as TypstPdfStandard;
4
5use typst_pdf::{PdfOptions, PdfStandards, Timestamp};
6
7use super::*;
8use crate::model::ExportPdfTask;
9
10pub struct PdfExport;
11
12impl<F: CompilerFeat> ExportComputation<F, TypstPagedDocument> for PdfExport {
13    type Output = Bytes;
14    type Config = ExportPdfTask;
15
16    fn run(
17        _graph: &Arc<WorldComputeGraph<F>>,
18        doc: &Arc<TypstPagedDocument>,
19        config: &ExportPdfTask,
20    ) -> Result<Bytes> {
21        let creation_timestamp = config
22            .creation_timestamp
23            .map(|ts| ts.to_utc_datetime().context("timestamp is out of range"))
24            .transpose()?
25            .unwrap_or_else(tinymist_std::time::utc_now);
26        // todo: this seems different from `Timestamp::new_local` which also embeds the
27        // timezone information.
28        let timestamp = Timestamp::new_utc(tinymist_std::time::to_typst_time(creation_timestamp));
29
30        let standards = PdfStandards::new(
31            &config
32                .pdf_standards
33                .iter()
34                .map(|standard| match standard {
35                    tinymist_world::args::PdfStandard::V_1_7 => typst_pdf::PdfStandard::V_1_7,
36                    tinymist_world::args::PdfStandard::A_2b => typst_pdf::PdfStandard::A_2b,
37                    tinymist_world::args::PdfStandard::A_3b => typst_pdf::PdfStandard::A_3b,
38                })
39                .collect::<Vec<_>>(),
40        )
41        .context_ut("prepare pdf standards")?;
42
43        // todo: Some(pdf_uri.as_str())
44        // todo: ident option
45        Ok(Bytes::new(typst_pdf::pdf(
46            doc,
47            &PdfOptions {
48                timestamp: Some(timestamp),
49                standards,
50                ..Default::default()
51            },
52        )?))
53    }
54}