Skip to content

URL with CJK characters becomes broken when exporting Typst document to PDF #6128

Open

Description

@hushpost

I have installed Typst-CLI on my Mac and am using the TinyMist extension in VSCode. When I use #link with a URL containing CJK characters, it seems that the URL becomes broken after exporting the document to PDF (it works fine when previewed in VSCode).

I’m not very knowledgeable about URL encoding, so I can’t explain it in detail, but I suspect that the issue lies in the process of converting non-ASCII URLs to ASCII, which seems to be handled differently than in a web browser.

Here’s the specific issue:

  1. The original URL used with #link: https://www.law.go.kr/법령/보건의료기본법/제3조
  2. The URL copied from the browser’s address bar after accessing the site: https://www.law.go.kr/%EB%B2%95%EB%A0%B9/%EB%B3%B4%EA%B1%B4%EC%9D%98%EB%A3%8C%EA%B8%B0%EB%B3%B8%EB%B2%95/%EC%A0%9C3%EC%A1%B0
  3. The URL in the PDF after exporting from Typst with the same link: https://www.law.go.kr/%C3%AB%C2%B2%C5%81%C3%AB%E2%82%AC%C2%B9/%C3%AB%C2%B3%C2%B4%C3%AA%C2%B1%C2%B4%C3%AC%C5%A1%C5%B8%C3%AB%C2%A3%E2%80%9E%C3%AA%C2%B8%C2%B0%C3%AB%C2%B3%C2%B8%C3%AB%C2%B2%C5%81/%C3%AC%E2%82%AC%C5%933%C3%AC%C2%A1%C2%B0

Activity

  1. PartyWumpus commented on Apr 1, 2025

    @PartyWumpus

    Looks to me like something in typst is reinterpreting UTF-8 bytes as latin-1 (8 hit ASCII).

  2. YDX-2147483647 commented on Apr 1, 2025

    @YDX-2147483647
    Contributor

    I cannot reproduce your issue with the following document in Sumatra PDF, Chromium or Firefox.

    #link("https://law.go.kr/법령/보건의료기본법/제3조")[Link]

    it works fine when previewed in VS Code

    VS Code is an Electron app, which uses Chromium. I guess that is why ok in VS Code.

    on my Mac

    Which PDF viewer do you use? Safari/Quartz? (Update: Yes I can reproduce it in Safari. Might be related to claudiodangelis/qrcp#331.)
    Could you try to open the exported PDF in VS Code and see if the link is decoded correctly?


    PS. Most LLMs are capable of decoding.

    Reply to Please decode this…

    The given string appears to be URL-encoded multiple times, likely due to improper encoding/decoding. Let's decode it step by step:

    Step 1: First URL Decoding

    Decoding %C3%AB%C2%B2%C5%81... (UTF-8 encoded bytes) once:

    벌뀹/보봼울룄ë°ë³¸ë²Œ/사Œ3ì¡°
    

    (This is still garbled because it was encoded multiple times.)

    Step 2: Second URL Decoding

    Decoding the result again:

    %EB%B2%8C%EB%AC%B8/%EB%B3%B4%EA%B1%B4%EC%A1%B8%EB%A3%9C%EA%B8%80%EB%B3%B8%EB%B2%8C/%EC%9C%A03%EC%9E%90
    

    (Now we see Korean characters encoded in UTF-8.)

    Step 3: Final Decoding (Korean Text)

    Decoding %EB%B2%8C%EB%AC%B8... gives:

    법문/보건조례로본법/제3자
    

    (This translates to: "Legal Text / Health Regulations / Third Party")

    Final Decoded URL (for law.go.kr)

    The full decoded path is likely:

    https://www.law.go.kr/법문/보건조례로본법/제3자
    

    (This appears to be a Korean legal document related to health regulations, specifically about a "third party.")

    Why This Happened

    • The URL was double-encoded (UTF-8 bytes were URL-encoded twice).
    • This often occurs when encoding is improperly handled in web requests.

    Would you like help finding the actual law on law.go.kr? Let me know!

  3. Enivex commented on Apr 1, 2025

    @Enivex
    Collaborator

    VS Code is an Electron app, which uses Chromium. I guess that is why ok in VS Code.

    More importantly, the preview is not PDF based

  4. added
    bug
    on May 21, 2025
  5. YDX-2147483647 commented on Aug 5, 2025

    @YDX-2147483647
    Contributor

    This issue can be mitigated by

    Links in bibliography should also share this feature.

    I did a preliminary investigation on where to implement it.

    Note

    Edit: Discord, 2025-08-05

    have an AsciiStr type in pdf-writer to enforce this on the type system level

    sounds plausible I think

    Maybe the AsciiChar ascii::Char in nightly rust? rust-lang/rust#110998

    what the html spec says there
    Should typst just fail then?

    • if it can be encoded, then it should probably just encode
    • if not, then it would have to fail

    Current status

    typst will not encode any URL when exporting to html or pdf.

    Example
    #link("https://law.go.kr/법령/보건의료기본법/제3조")[kr]
    #link("https://w3c.github.io/clreq/README.zh-Hans.html#讨论")[zh-Hans]
    #link("https://w3c.github.io/clreq/README.zh-Hant.html#討論")[zh-Hant]
    #link("https://ja.wikipedia.org/wiki/アルベルト・アインシュタイン")[ja]
    
    #link("https://w3c.github.io/clreq/README.zh-Hans.html#%E8%AE%A8%E8%AE%BA")[Encoded]
    <!DOCTYPE html>
    <html>
      <head>
        <meta charset="utf-8">
        <meta name="viewport" content="width=device-width, initial-scale=1">
      </head>
      <body>
        <p><a href="https://law.go.kr/법령/보건의료기본법/제3조">kr</a> <a href="https://w3c.github.io/clreq/README.zh-Hans.html#讨论">zh-Hans</a> <a href="https://w3c.github.io/clreq/README.zh-Hant.html#討論">zh-Hant</a> <a href="https://ja.wikipedia.org/wiki/アルベルト・アインシュタイン">ja</a></p>
        <p><a href="https://w3c.github.io/clreq/README.zh-Hans.html#%E8%AE%A8%E8%AE%BA">Encoded</a></p>
      </body>
    </html>
    $ pdfinfo -url main.pdf
    Page  Type          URL
       1  Annotation    https://law.go.kr/법령/보건의료기본법/제3조
       1  Annotation    https://w3c.github.io/clreq/README.zh-Hans.html#讨论
       1  Annotation    https://w3c.github.io/clreq/README.zh-Hant.html#討論
       1  Annotation    https://ja.wikipedia.org/wiki/アルベルト・アインシュタイン
       1  Annotation    https://w3c.github.io/clreq/README.zh-Hans.html#%E8%AE%A8%E8%AE%BA

    krilla's API is String here, and the string will be serialized as raw bytes, without percent encoding.

    impl LinkAction {
        fn serialize(&self, mut action: pdf_writer::writers::Action) {
            action
                .action_type(ActionType::Uri)
                .uri(Str(self.uri.as_bytes()));
        }
    }

    hayagriva will automatically encode URLs, even when displaying.

    #bibliography(
      bytes(
        ```bib
        @online{key,
          url = {https://ja.wikipedia.org/wiki/アルベルト・アインシュタイン},
        }
        ```.text,
      ),
      full: true,
    )
    Image

    Spec

    In short, non-ASCII strings are not valid URIs, but they are easier for human to understand.

    PDF spec — ISO 32000-2:2020(E)

    Table 210 Additional entries specific to a URI action, in Section 12.6.4.8 URI actions:

    (URIs are described in Internet RFC 3986.)

    • Key: URI
    • Type: ASCII string
    • Value: (Required) The uniform resource identifier to resolve, encoded in UTF-8.
    Screenshot Image

    URI spec — RFC 3986

    2.3. Unreserved Characters:

    Characters that are allowed in a URI but do not have a reserved purpose are called unreserved.
    unreserved = ALPHA / DIGIT / "-" / "." / "_" / "~"

    2.4. When to Encode or Decode:

    Under normal circumstances, the only time when octets within a URI
    are percent-encoded is during the process of producing the URI from
    its component parts. This is when an implementation determines which
    of the reserved characters are to be used as subcomponent delimiters
    and which can be safely used as data. Once produced, a URI is always
    in its percent-encoded form.

    Unicode tech standard — Proposed Draft UTS #58: Unicode Link Detection and Serialization Formatting

    This draft standard describes a Minimal Escaping Algorithm.

    The goal is to be able to generate a serialized form of a URL that:

    1. is correctly parsed by modern browsers and other devices
    2. minimizes the use of percent-escapes
    3. is completely link-detected when isolated.

    Update on 2026-04-22: The draft becomes a Unicode 17.0 standard on 2026-02-02.

    CommonMark Spec

    An absolute URI, for these purposes, consists of a scheme followed by a colon (:) followed by zero or more characters other than ASCII control characters, space, <, and >. If the URI includes these characters, they must be percent-encoded (e.g. %20 for a space).

    Reference implementation

    mdbook and pulldown-cmark: Encode automatically

    [kr](https://law.go.kr/법령/보건의료기본법/제3조)
    [zh-Hans](https://w3c.github.io/clreq/README.zh-Hans.html#讨论)
    [zh-Hant](https://w3c.github.io/clreq/README.zh-Hant.html#討論)
    [ja](https://ja.wikipedia.org/wiki/アルベルト・アインシュタイン)
    
    [Encoded](https://w3c.github.io/clreq/README.zh-Hans.html#%E8%AE%A8%E8%AE%BA)

    pulldown-cmark 0.13.0 (latest) and 0.9.6 (used by main) will turn the above markdown into the following html.

    <p><a href="https://law.go.kr/%EB%B2%95%EB%A0%B9/%EB%B3%B4%EA%B1%B4%EC%9D%98%EB%A3%8C%EA%B8%B0%EB%B3%B8%EB%B2%95/%EC%A0%9C3%EC%A1%B0">kr</a>
    <a href="https://w3c.github.io/clreq/README.zh-Hans.html#%E8%AE%A8%E8%AE%BA">zh-Hans</a>
    <a href="https://w3c.github.io/clreq/README.zh-Hant.html#%E8%A8%8E%E8%AB%96">zh-Hant</a>
    <a href="https://ja.wikipedia.org/wiki/%E3%82%A2%E3%83%AB%E3%83%99%E3%83%AB%E3%83%88%E3%83%BB%E3%82%A2%E3%82%A4%E3%83%B3%E3%82%B7%E3%83%A5%E3%82%BF%E3%82%A4%E3%83%B3">ja</a></p>
    <p><a href="https://w3c.github.io/clreq/README.zh-Hans.html#%E8%AE%A8%E8%AE%BA">Encoded</a></p>
    Rust test code
    use pulldown_cmark::{Options, Parser};
    
    fn main() {
        let markdown_input = r"
    [kr](https://law.go.kr/법령/보건의료기본법/제3조)
    [zh-Hans](https://w3c.github.io/clreq/README.zh-Hans.html#讨论)
    [zh-Hant](https://w3c.github.io/clreq/README.zh-Hant.html#討論)
    [ja](https://ja.wikipedia.org/wiki/アルベルト・アインシュタイン)
    
    [Encoded](https://w3c.github.io/clreq/README.zh-Hans.html#%E8%AE%A8%E8%AE%BA)
    ";
    
        let parser = Parser::new_ext(markdown_input, Options::empty());
    
        let mut html_output = String::new();
        pulldown_cmark::html::push_html(&mut html_output, parser);
    
        println!("{html_output}");
    }

    pandoc: Never encode

    pandoc 3.5 does not encode when converting from the above markdown to html5 or pdf.

    The behaviour is basically the same as typst 0.13.1, and suffers the same issue on Safari.

    LaTeX and hyperref: Never encode

    % ! TeX program = xelatex or lualatex
    \documentclass{article}
    \usepackage{hyperref}
    
    \begin{document}
    
    \href{https://law.go.kr/법령/보건의료기본법/제3조}{kr}
    \href{https://w3c.github.io/clreq/README.zh-Hans.html#讨论}{zh-Hans}
    \href{https://w3c.github.io/clreq/README.zh-Hant.html#討論}{zh-Hant}
    \href{https://ja.wikipedia.org/wiki/アルベルト・アインシュタイン}{ja}
    
    \href{https://w3c.github.io/clreq/README.zh-Hans.html#%E8%AE%A8%E8%AE%BA}{Encoded}
    
    \end{document}
    $ pdfinfo -url main.pdf
    Page  Type          URL
       1  Annotation    https://law.go.kr/법령/보건의료기본법/제3조
       1  Annotation    https://w3c.github.io/clreq/README.zh-Hans.html#讨论
       1  Annotation    https://w3c.github.io/clreq/README.zh-Hant.html#討論
       1  Annotation    https://ja.wikipedia.org/wiki/アルベルト・アインシュタイン
       1  Annotation    https://w3c.github.io/clreq/README.zh-Hans.html#%E8%AE%A8%E8%AE%BA

    The behaviour is basically the same as typst 0.13.1, and suffers the same issue on Safari.

    However, biblatex automatically encodes URLs, even when displaying. Its behaviour is the same as hayagriva.

  6. added
    pdf
    on Sep 19, 2025
  7. squan3337-eng commented on Dec 18, 2025

    @squan3337-eng
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

    bugpdf

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions