Skip to content

Failed to convert PDF file: the SVG's nesting depth is too high #6051

Closed

Description

@Andrew15-5

Description

To work around #6035, I created a package that should've solved all of my problems and I could just move on. But now apparently my diagram has too much stuff in it (but it's basically a cetz canvas wrapper), and it gives me this error:

error: Failed to convert PDF file: The SVG's nesting depth is too high..

Which happens on this check: https://github.com/typst/svg2pdf/blob/24acb191cfcc3d5b1ca53ea29c42293739c930cd/src/util/helper.rs?plain=1#L60.

Indeed, increasing the number results in svg2pdf not erroring anymore.

The MRE is actually surprisingly simple:

// svg.typ
#show: box
#show: box
#show: box
#show: box
#show: box
#show: box
#show: box
#show: box
#show: box
typst c -f svg svg.typ
// main.typ
#image("svg.svg")
typst c main.typ

@LaurenzV confirmed that it should be considered a bug (and I agree). I use Typst to create an image that is used in a Typst document that should compile without a hitch. Typst + Typst shouldn't give an error.

Cc @johannes-wolf (just so you are aware of such an issue)

Reproduction URL

No response

Operating system

Linux

Typst version

  • I am using the latest version of Typst

Activity

  1. added
    bug
    on Mar 12, 2025
  2. Andrew15-5 commented on Mar 12, 2025

    @Andrew15-5
    ContributorAuthor

    The MRE for CeTZ would be something like this:

    #show: box
    #show: box
    #show: box // This already triggers the error.
    // #show: box
    // #show: box
    // #show: box
    // #show: box
    // #show: box
    // #show: box
    
    #import "@preview/cetz:0.3.4"
    
    #let draw-node(center, body) = {
      let (w, _) = measure(body).values()
      cetz.draw.content(center, block(width: w, body))
    }
    
    #context cetz.canvas({
      import cetz.draw: *
      draw-node((0, 0))[text]
    })
  3. Andrew15-5 commented on Mar 12, 2025

    @Andrew15-5
    ContributorAuthor

    I'm pretty lucky with this one. I used place(figure(box(image()))) pattern before, but thankfully, if I remove the box() call, then it doesn't error anymore, and guess what? With cetz I can make a rect wrapper around all canvas content, and it doesn't increase the nesting. So I used the group(name: "g", stuff); rect("g.south-west", "g.north-east") pattern and it works like a charm!

    As a result, the new 3 PNG images add 7.51 MiB to the document file + are not sharp + increase page loading time, but the new 3 SVG images add only 1.88 MiB + are razor-sharp + make pages load fast.

    So in the end, I indeed was able to make my plan work, and all it took is a DOT language parser and plotter written in pure Typst. Unfortunately, the time it takes to compile my diagrams is about 0.5 s/diagram, which is 3 s for 6 of them. Which gives me about 4.8 s of total compilation time for my PDF document. I guess it's acceptable, as currently it's probably by far the best option anyway (for my relatively simple diagrams).

    P.S. Since I have screenshots in those pages, their size is still not very lean, but I can't really do anything about it, at least right now. Maybe I'll compress them in the future (or maybe they are already compressed enough).

  4. added
    svg
    on Mar 12, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugsvg

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions