vscode-markdown icon indicating copy to clipboard operation
vscode-markdown copied to clipboard

Exploration: export as PDF (no satisfatory solution)

Open yzhang-gh opened this issue 3 years ago • 14 comments

Updated: 2024-04

Considering the complexity and the foreseeable outcome of the current possible solutions, we may not plan to implement this feature until there are better ways.

Users can use Markdown PDF for this missing feature.


  • [ ] Choose a backend
    • Puppeteer. Most promising.
      • An issue is it doesn't seem to generate bookmarks for the headings
      • https://github.com/yzane/vscode-markdown-pdf
      • https://github.com/BlueHatbRit/mdpdf
    • User-installed Chrome. (Not sure whether there is any difference from Puppeteer, e.g. APIs, rendering.)
      • https://chromedevtools.github.io/devtools-protocol/tot/Page/#method-printToPDF
    • wkhtmltopdf
      • A big problem is CJK fonts are not applied.
    • WeasyPrint
      • need to check
  • [ ] Download (on demand) binaries for different OSes. See vscode-ltex for an example.
    • Handle possible network issues (fallback URLs, proxies etc.)
  • [ ] Extra: provide a few presets (themes) @yzhang-gh

cc #270.


install.ts https://github.com/puppeteer/puppeteer/blob/main/src/node/install.ts


Help wanted

In case anyone knows how Typora (Electron-based) generates bookmarks for headings.

https://www.electronjs.org/docs/api/web-contents#contentsprinttopdfoptions


Found a Python-based TOC generation tool https://github.com/Krasjet/pdf.tocgen

yzhang-gh avatar Jan 02 '21 12:01 yzhang-gh

Note that Chrome's print-to-pdf functionality does not always retain working hyperlinks in the output PDF, see here: https://superuser.com/questions/809627/print-webpage-to-pdf-with-working-hyperlinks

Retaining hyperlinks might be a desirable feature of markdown-to-pdf, though.

ComFreek avatar Feb 14 '21 14:02 ComFreek

As far as I know, it works well in general. (The superuser link looks quite outdated.)

yzhang-gh avatar Feb 14 '21 15:02 yzhang-gh

Are you aware of https://github.com/yzane/vscode-markdown-pdf?

dschuessler avatar Feb 21 '21 18:02 dschuessler

Yep, I know it from the very beginning. It uses Puppeteer now so it also has the problem that it doesn't generate bookmarks.

yzhang-gh avatar Feb 22 '21 02:02 yzhang-gh

Why not consider pandoc? It works well with bookmarks. Check out this vscode-pandoc. Note that Typora uses pandoc to export to many formats including epub, rtf, LaTeX, docx, etc. but not PDF and HTML. I guess Typora uses the print API of Electron to export PDF? There is an open source markdown editor similar to Typora called Mark Text which seems to use this API.

Besides, FYI Markdown Extended uses Puppeteer and nbconvert of Jupyter Notebook uses both pandoc and Puppeteer (provides two versions of PDF export).

MatrixRanger98 avatar May 10 '21 17:05 MatrixRanger98

pandoc

The PDF is generated by TeX so the layout is totally different. If users want that, they already have vscode-pandoc as you mentioned...

I guess Typora uses the print API of Electron to export PDF? There is an open source markdown editor similar to Typora called Mark Text which seems to use this API.

Mark Text does use the API from Electron. But I guess it doesn't have bookmarks (unlike Typora).

https://github.com/marktext/marktext/blob/0fa6c33c44e92d0d1729d3d557d02ac7db87e73f/src/main/menu/actions/file.js#L72

yzhang-gh avatar May 11 '21 03:05 yzhang-gh

I see what you mean. They say nothing about what Typora uses for its PDF export on their website. Maybe can try WeasyPrint. This one works well with both bookmarks and CJK.

MatrixRanger98 avatar May 11 '21 04:05 MatrixRanger98

Note that this one directly uses the python library pydyf to create PDFs and does not require a chromium.

MatrixRanger98 avatar May 11 '21 04:05 MatrixRanger98

WeasyPrint

Interesting. Adding to the list.

yzhang-gh avatar May 11 '21 04:05 yzhang-gh

I just wanted to say that I am so happy this is under consideration because I love this extension so much, but what annoys me is having to export everything to HTML, then open the HTML in Firefox, then save the page as a PDF, and in many cases then have to use another program to merge the pdfs together into one file. I just wanted to say that I'm so happy this issue is open and I hope it's progressing well! Thank you for your work, this is a great extension!

scott-parkhill avatar Sep 08 '21 15:09 scott-parkhill

hey @yzhang-gh any update about this nice feature?

javix64 avatar Apr 11 '24 13:04 javix64

Hi, any updates here?

It will be really help if pdf can be generated.

One more issue about the markdown-pdf extension is the lack of math formula conversion support.

Although this issue can be fixed by adding following code, but this fix doesnt work in private net.

<script type="text/javascript" src="http://cdn.mathjax.org/mathjax/latest/MathJax.js?config=TeX-AMS-MML_HTMLorMML"></script>
<script type="text/x-mathjax-config"> MathJax.Hub.Config({ tex2jax: {inlineMath: [['$', '$']]}, messageStyle: "none" });</script>

Any plan to add support?

It would be really nice to install only one extension for markdown rather than lots of different extensions.😂

ruiyuanlu avatar Apr 19 '24 01:04 ruiyuanlu

Hi, thank both of you for the interest. As you may already know, (up to now) there is no good API for the PDF related functionality. And different external tools have different issues, any of the solutions not only requires great effort but also is not that satisfactory. It might be a better idea to make improvements to the existing markdown-pdf extension rather than implementing a new one.

yzhang-gh avatar Apr 19 '24 07:04 yzhang-gh

Thanks for answering @yzhang-gh, I know what you mean. Thanks for your effort and your work, excellent work!

I guess it is a good idea if you write explicity that this addon does not support PDF (and write which one can do it, if you want) in the README.md section and also is not planned, just for clarify the situation like many people open the issue about this topic.

Have a good developing time!

javix64 avatar Apr 19 '24 17:04 javix64