A place to cache linked articles (think custom and personal wayback machine)
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

index.md 6.2KB

2 jaren geleden
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. title: How I create fancy PDF-documents in Markdown
  2. url: https://hund.tty1.se/2021/07/14/how-i-create-fancy-pdf-documents-in-markdown.html
  3. hash_url: 0c60682eeb1edfaf2c6c13cd30cce60d
  4. <p class="date">Wednesday, July 14, 2021</p>
  5. <p>I haven’t had the need to create any ‘fancy’ document in ages, but the other day I had to create a PDF-document with fancy formatting for a work related thing.</p>
  6. <p>After some testing of various solutions and tools, I ended up with a setup using <a href="https://pandoc.org">Pandoc</a>, <a href="https://weasyprint.org/">WeasyPrint</a> and <a href="https://en.wikipedia.org/wiki/Markdown">Markdown</a>. I consider Markdown to be the best markup language and I use it for everything!</p>
  7. <div class="grid-container">
  8. <div class="grid-item single">
  9. <a href="https://hund.tty1.se/img/pandoc-pdf.png">
  10. <img src="https://hund.tty1.se/img/t/pandoc-pdf.png" alt="">
  11. </a>
  12. <figcaption>My example document</figcaption>
  13. </div>
  14. </div>
  15. <p>The source for this PDF-document looks like this:</p>
  16. <div class="language-plaintext highlighter-rouge"><div class="highlight"><pre class="highlight"><code># H1 Header
  17. I really like using Markdown.
  18. 1. First item
  19. 2. Second item
  20. 3. Third item
  21. 4. Fourth item
  22. &gt; This is some quoted text.
  23. ```sh
  24. #!/bin/sh
  25. echo "This is a code block."
  26. ```
  27. </code></pre></div></div>
  28. <p>And this is the CSS I used:</p>
  29. <div class="language-css highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="nd">:root</span> <span class="p">{</span>
  30. <span class="py">--blue</span><span class="p">:</span> <span class="m">#3465A4</span><span class="p">;</span>
  31. <span class="p">}</span>
  32. <span class="k">@font-face</span> <span class="p">{</span>
  33. <span class="nl">font-family</span><span class="p">:</span> <span class="s2">'NotoSans'</span><span class="p">;</span>
  34. <span class="nl">src</span><span class="p">:</span> <span class="sx">url('NotoSans-Condensed.ttf')</span> <span class="n">format</span><span class="p">(</span><span class="s2">'ttf'</span><span class="p">)</span>
  35. <span class="p">}</span>
  36. <span class="nt">html</span> <span class="p">{</span>
  37. <span class="nl">font-family</span><span class="p">:</span> <span class="s2">'NotoSans'</span><span class="p">;</span>
  38. <span class="nl">line-height</span><span class="p">:</span> <span class="m">1.45em</span><span class="p">;</span>
  39. <span class="p">}</span>
  40. <span class="nt">h1</span> <span class="p">{</span>
  41. <span class="nl">font-size</span><span class="p">:</span> <span class="m">26px</span><span class="p">;</span>
  42. <span class="nl">color</span><span class="p">:</span> <span class="n">var</span><span class="p">(</span><span class="n">--blue</span><span class="p">);</span>
  43. <span class="nl">padding-bottom</span><span class="p">:</span> <span class="m">0.15em</span><span class="p">;</span>
  44. <span class="nl">border-bottom</span><span class="p">:</span> <span class="m">1px</span> <span class="nb">solid</span> <span class="m">#CCC</span><span class="p">;</span>
  45. <span class="p">}</span>
  46. <span class="nt">pre</span> <span class="p">{</span>
  47. <span class="nl">background-color</span><span class="p">:</span> <span class="n">rgba</span><span class="p">(</span><span class="m">0</span><span class="p">,</span><span class="m">0</span><span class="p">,</span><span class="m">0</span><span class="p">,</span><span class="m">0.05</span><span class="p">);</span>
  48. <span class="nl">border</span><span class="p">:</span> <span class="m">1px</span> <span class="nb">solid</span> <span class="n">rgba</span><span class="p">(</span><span class="m">0</span><span class="p">,</span><span class="m">0</span><span class="p">,</span><span class="m">0</span><span class="p">,</span><span class="m">0.1</span><span class="p">);</span>
  49. <span class="nl">padding</span><span class="p">:</span> <span class="m">1em</span><span class="p">;</span>
  50. <span class="nl">font-size</span><span class="p">:</span> <span class="m">12px</span><span class="p">;</span>
  51. <span class="p">}</span>
  52. <span class="nt">blockquote</span> <span class="p">{</span>
  53. <span class="nl">background-color</span><span class="p">:</span> <span class="n">rgba</span><span class="p">(</span><span class="m">0</span><span class="p">,</span><span class="m">0</span><span class="p">,</span><span class="m">0</span><span class="p">,</span><span class="m">0.05</span><span class="p">);</span>
  54. <span class="nl">border-left</span><span class="p">:</span> <span class="m">5px</span> <span class="nb">solid</span> <span class="n">rgba</span><span class="p">(</span><span class="m">0</span><span class="p">,</span><span class="m">0</span><span class="p">,</span><span class="m">0</span><span class="p">,</span><span class="m">0.1</span><span class="p">);</span>
  55. <span class="nl">padding</span><span class="p">:</span> <span class="m">0.1em</span> <span class="m">0.75em</span><span class="p">;</span>
  56. <span class="p">}</span>
  57. </code></pre></div></div>
  58. <p>And this is the command I used to render the document:</p>
  59. <div class="language-plaintext highlighter-rouge"><div class="highlight"><pre class="highlight"><code>$ pandoc --pdf-engine weasyprint -c style.css demo.md -s -o demo.pdf
  60. </code></pre></div></div>
  61. <h2 id="installation">Installation</h2>
  62. <p>To make this work you need to install Pandoc and WeasyPrint. In Gentoo the packages are called <code class="language-plaintext highlighter-rouge">app-text/pandoc</code> and <code class="language-plaintext highlighter-rouge">dev-python/weasyprint</code>.</p>
  63. <p>With that said. I did not install Pandoc from source myself. It simply requires way too many packages for my liking (when compiled from source):</p>
  64. <div class="language-plaintext highlighter-rouge"><div class="highlight"><pre class="highlight"><code># emerge pandoc
  65. [...]
  66. Total: 133 packages (133 new), Size of downloads: 165,258 KiB
  67. </code></pre></div></div>
  68. <p>I instead used a binary package that’s available from their <a href="https://github.com/jgm/pandoc/releases/">GitHub-page</a>. The archive weights in at about 14 MB and unpacked at about 68 MB. I then just put the folder in <code class="language-plaintext highlighter-rouge">~/.local/bin/</code> and created a symbolic link to the binary file.</p>