Find page of metadata marker in pdf

This is a later add-on to:

The method described there works when I want to insert one pdf at a given position. When I want to insert another document, it does no longer work, because the typst-query for the inserted marker refers to the position in the original document, not in the extended pdf.

So I cannot use the typst query, but must find the marker position in the pdf document itself. However, I have not been able to find it with cpdf or pdftk. Current workaround is to search for some H1 header, but this is not reliable, I would prefer to use an invisible marker.

As I understand it, you are merging compiled PDFs, is that correct? Presumably, you are then inserting entire pages into your document, so would it be possible for you to offset the query values by the number of pages already inserted?

What I mean is, suppose you have your main document main.pdf and wish to insert 3 pages from both documents a.pdf and b.pdf. If the query command tells you you should insert them at pages 4 and 8 of main.pdf, you would insert a.pdf onto page 4 and b.pdf onto page 8 + 3 = 11.

Otherwise, typst allows you to insert PDFs into your document natively via the image function

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


#insert-pdf("insertme.pdf", (1,2,3))

Thanks, I had tried the “count pages” version, but it was a bit ugly, so looking for a header was easier.

I will have a second look at the image function though. I was aware of it, but had wrongly had understood that this one would insert the pages as scanned images, which was not acceptable.

Tried, works perfectly. Do you know how to get the number of pages, when I always want to include all?
See also : muchpdf 0.1.1 – Typst Universe

Look like using an external tool to get the number of pages is easier:

1 Like

Indeed, this is what i’d recommend since typst does not support this natively. Once you get the number of pages, you can pass them as inputs with --input pages-file-a=<number>, for example.

The linked issue does include a hacky way of extracting the number of pages from the pdf by user ghost, but obviously I cannot guarantee that this will always work.

You can even use the muchpdf package to get the number of pages, but this is slow

#muchpdf(read("main.pdf", encoding: none)).children.len()