tinymist_task/compute/
pdf.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
use tinymist_std::time::ToUtcDateTime;
pub use typst_pdf::pdf;
pub use typst_pdf::PdfStandard as TypstPdfStandard;

use typst_pdf::{PdfOptions, PdfStandards, Timestamp};

use super::*;
use crate::model::ExportPdfTask;

pub struct PdfExport;

impl<F: CompilerFeat> ExportComputation<F, TypstPagedDocument> for PdfExport {
    type Output = Bytes;
    type Config = ExportPdfTask;

    fn run(
        _graph: &Arc<WorldComputeGraph<F>>,
        doc: &Arc<TypstPagedDocument>,
        config: &ExportPdfTask,
    ) -> Result<Bytes> {
        let creation_timestamp = config
            .creation_timestamp
            .map(|ts| ts.to_utc_datetime().context("timestamp is out of range"))
            .transpose()?
            .unwrap_or_else(tinymist_std::time::utc_now);
        // todo: this seems different from `Timestamp::new_local` which also embeds the
        // timezone information.
        let timestamp = Timestamp::new_utc(tinymist_std::time::to_typst_time(creation_timestamp));

        let standards = PdfStandards::new(
            &config
                .pdf_standards
                .iter()
                .map(|standard| match standard {
                    tinymist_world::args::PdfStandard::V_1_7 => typst_pdf::PdfStandard::V_1_7,
                    tinymist_world::args::PdfStandard::A_2b => typst_pdf::PdfStandard::A_2b,
                    tinymist_world::args::PdfStandard::A_3b => typst_pdf::PdfStandard::A_3b,
                })
                .collect::<Vec<_>>(),
        )
        .context_ut("prepare pdf standards")?;

        // todo: Some(pdf_uri.as_str())
        // todo: ident option
        Ok(Bytes::new(typst_pdf::pdf(
            doc,
            &PdfOptions {
                timestamp: Some(timestamp),
                standards,
                ..Default::default()
            },
        )?))
    }
}