Preview of multiple pages document in VS code using Typstry.jl

I’m looking for a way to deal with multiple pages preview when using Typstry.jl in VS code with the Julia extension (using PrettyTables typst backend in this example just for fun).

# Init
using Typstry, PrettyTables, DataFrames
io = IOBuffer()
pretty_table(io,DataFrame(a=1:15),backend = :typst)
pt_df = String(take!(io))

If I create a single “A4” page document with height:auto, I can see the preview in the Julia Plots but the generated PDF will be an unusable single page document :

single_page_doc = TypstString(TypstText("""
#set page("a4", height:auto)
$pt_df
#lorem(500)
"""))
render(single_page_doc;input="ok.typ",output="ok.pdf",open=false)

If I want a document with more than 1 page (without height: auto), there’s no preview of the document in Julia Plots since a TypstCommandError is thrown, but the PDF will be fine with multiple pages :

# ERROR: TypstCommandError: failed to run a `TypstCommand (["compile", "--font-path", "....julia/artifacts/...", "--format", "png", "-", "-"])
multiple_page_doc = TypstString(TypstText("""
#set page("a4")
$pt_df
#lorem(500)
"""))
render(multiple_page_doc;input="bug.typ",output="bug.pdf",open=false)

So now I’m setting the page’s height to auto to have a preview and remove it when I want to render the final PDF…
Is there a better way to have a multiple page preview working using Typstry.jl in VS code without using other typst extension ?

VSCode does this natively using a terminal emulator. Not all support graphic rendering in the first place and AFAIK none of those that do are able to cope reliably with multipage images. The pdf comes in as stdio in a l o o o n g string. stream of pixels so the buffer gets clobbered. I’ve heard tell that Some terminals implement protocols that enable direct image rendering. Some terminals implement protocols that enable direct image rendering. I’ve seen reports that VSCode extensions like PDF Viewer (by mathematic) or PDF Preview embed Mozilla’s pdf.js library right into the IDE. This gives you a native-feeling PDF reader with full layout controls, a sidebar index, smooth scrolling, and text highlighting, completely bypassing the terminal’s limitations. For my money if composing in VSCode or another IDE running a Typst watch command is the process to display on save in a different window is the way to go.

Hi there! I ran the code you provided in a new environment and am unable to reproduce the error. Would you be able to provide a script that produces the error you describe? I’m happy to help :slight_smile:

(@v1.12) pkg> activate --temp
  Activating new project at `/tmp/jl_wpoM7S`

(jl_wpoM7S) pkg> add Typstry, DataFrames, PrettyTables
   Resolving package versions...
    Updating `/tmp/jl_wpoM7S/Project.toml`
    ...

julia> using Typstry, PrettyTables, DataFrames

julia> io = IOBuffer();

julia> pretty_table(io,DataFrame(a=1:15),backend = :typst)

julia> pt_df = String(take!(io));

julia> single_page_doc = TypstString(TypstText("""
       #set page("a4", height:auto)
       $pt_df
       #lorem(500)
       """));

julia> render(single_page_doc;input="ok.typ",output="ok.pdf",open=false)

julia> multiple_page_doc = TypstString(TypstText("""
       #set page("a4")
       $pt_df
       #lorem(500)
       """));

# this did not error for me
julia> render(multiple_page_doc;input="bug.typ",output="bug.pdf",open=false)

but the PDF will be fine with multiple pages

Oh, is it that the PDF renders (as my attempt shows) but some other environment throws the error? Could you elaborate on that? I do not use VSCode and do not know where to look.

I think I’ve understand what the problem is.

The error is not thrown when rendering the pdf but when VSC tries to display a preview of a TypstString of a multipage document using the Typstry.jl > utilities.jl > show_render() function.
This function tries to compile a png (that will be used as a preview of the document) using a TypstCommand equivalent to this :

run(typst`compile multipage.typ --format png`)

… when a multipage document should be compiled to png with a page number template in the output path or given a specific page :

run(typst`compile multipage.typ {p}-{t}.png --format png`)
run(typst`compile multipage.typ --format png --pages 1`)

To summarise, VS code preview works fine with a singlepage document by using this command in show_render :

single_page_doc = TypstString(TypstText("""
#set page("a4", height:auto) 
#lorem(1000)
"""))
write("singlepage.typ",single_page_doc)
run(typst`compile singlepage.typ --format png`) # generate a multipage.png correctly

But when using the same command for a multipage document, an error: cannot export multiple images without a page number template ({p}, {0p}) in the output path is thrown, and no png preview is generated :

multiple_page_doc = TypstString(TypstText("""
#set page("a4")
#lorem(1000)
"""))

write("multipage.typ",multiple_page_doc)
run(typst`compile multipage.typ --format png`) # TypstComandError : failed to run with typst error : cannot export multiple images without a page number template ({p}, {0p}) in the output path

# where these command works fine for multipage document
run(typst`compile multipage.typ --format png --pages 1`) # 1-1.png generated
run(typst`compile multipage.typ {p}-{t}.png --format png`) # 1-2.png and 2-2.png generated

Thank you for investigating further! I can reproduce with your example and using render and a png output file. v0.7.0 switched to use stdin and stdout instead of writing to a file (source). This method is used by show(::IO, ::MIME, ::T). Since the data must be written to the IO, the support for multiple pages is strictly dependent on the format. That is, it can render a multi-page PDF but not PNG or SVG because those formats fundamentally require multiple files.

I believe you can correctly generate a multi-page PDF. Could you try to install an extension that supports those, such as vscode-pdf? I would suggest inquiring with the Julia VSCode extension about multi-page PDF support.