Inserting a pdf into my document (again, sorry)

Using Quarto/typst

In all my reports, I have to insert separate pdf files; preferably, keeping page numbering correct, but that’s a secondary issue that I know is problematic. Since some of the other threads drifted off, I start a new one here in the hope to get a state-of-the-art solution.

Threads

I have read the relevant threads:

and the use introduction of “eval” instead of query.

Insert as image

I currently use the following workaround

#let insert-pdf(document-name, pages) = context {
  for i in range(1, pages+1) {
    move(
      image(
        document-name, 
        page: i, 
        width: page.width,
        height: page.height
      ),
      dx: -here().position().x,
      dy: -here().position().y,
    )
  }
}

but this uses images and my client does not like these non-searchable images.

Using pdftk

The alternative is to use pdftk, with a line in .ps1 like the following:

pdftk A=$inp B=$merge_with cat A1-$page B A$page1-end output $out

This works, but I have to find know $page in advance. I have a label into the document:

#label("fachinformation_pdf")

but I could not find the page of the label by program.

Finding label page

Claude suggests using

typst eval "query(<fachinformation>).first().location().page()" --in index.typ

This give a strange error message:

typst eval "query(<fachinformation>).first().location().page()" --in index.typ
error: array is empty
  ┌─ <input-expression>:1:0
  │
1 │ query(<fachinformation>).first().location().page()
  │ ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

warning: unknown font family: font awesome 6 free
   ┌─ @preview/fontawesome:0.5.0/lib-impl.typ:51:10
   │
51 │     font: default_fonts, // If you see warning here, please check whether the FA font is installed
   │           ^^^^^^^^^^^^^

Note: the report builds without problems

Questions

  1. Any better alternatives to insert a pdf as of today?
  2. How to find the location of a label with eval?
  3. Secondary (maybe impossible): Get page numbering right? Note that the pdf is inserted into the middle of the main document.
1 Like

Inserting a PDF as an image results in selectable and searchable text – at least in my PDF viewers: VS Code (Chromium), Firefox and Okular all behave the same in this regard. Are there other PDF viewers in which the inserted PDF is not selectable/searchable? That would probably be considered a bug (either in that viewer or Typst, depending on who is right about the PDF contents).

I think the following insert-pdf is cleaner though, and doesn’t require context:

#let insert-pdf(document-name, pages) = {
  for i in range(1, pages+1) {
    page(margin: 0pt, image(
      document-name,
      page: i,
      width: 100%,
      height: 100%,
    ))
  }
}

Since your goal is to fill the whole page, calling page() explicitly works well here. If you want to potentially preserve the pages’ sizes, you could also use this instead:

#let insert-pdf(document-name, pages) = {
  for i in range(1, pages+1) {
    page(width: auto, height: auto, margin: 0pt, image(
      document-name,
      page: i,
    ))
  }
}

Querying a label does not really find labels, it finds elements tagged with labels. You may want to write #metadata(none) #label("fachinformation_pdf"), or equally #metadata(none) <fachinformation_pdf> instead, since if you attach a label to a long paragraph, the location (and page) may be earlier than you expect.

It wrote fachinformation instead of fachinformation_pdf, otherwise the query looks right :slight_smile:

2 Likes

It wrote fachinformation instead of fachinformation_pdf , otherwise the query looks right

Krrr… That the curse of AI, when you don’t check it. Could have happened to a human, though.

Inserting a PDF as an image results in selectable and searchable text –

Does not work for me. And the text is semi-good:

Check with pdf-Exchange. Search for “blassgelbes” does not return anything

hmm… maybe it has something to do with the source PDF, does it contain OCR text? If it is not confidential, it would be cool if you could share a sample of a PDF that inhibits this problem (here or in a Github issue). Embedding Typst-produced PDFs using image() definitely doesn’t have either the rendering quality or selection issues.

And again, mea culpa, but a thing I learned: the example really was a scan, e.g. from
AIPS - Einzelabfrage, try “Addaven” (not my example)

On my computer, I have two drivers for pdf: “Save as pdf” gives nice native pdf, while the pdfXChange produces the 2.5x larger scans. If at all, I would have expected the opposite.

I have not tried how this works out with “image”, but I am sure you are right.

Sorry for the noise. Hopefully, you “image” example will help others too.

1 Like

Implemented and works great. My variant below

#let insert-pdf(document-name, pages, scale: 100%, margin: 10pt) = {
  for i in range(1, pages+1) {
    page(margin: margin, image(
      document-name,
      page: i,
      width: scale,
      height: scale,
    ))
  }
}
1 Like