A place to cache linked articles (think custom and personal wayback machine)
Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.

index.md 5.2KB

před 2 roky
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. title: Creating a Nice-Looking PDF with pandoc
  2. url: https://plaintextproject.online/articles/2022/04/06/pdf.html
  3. hash_url: 4d01ca34a54c4be19535df17fdf0c9d2
  4. <p>If you’re familiar with <a href="https://pandoc.org" target="_blank">pandoc</a>, you know how useful and flexible it is. One of the many formats that you can convert to using pandoc is PDF.</p>
  5. <p>Before version 2 of the tool, you needed the TeX typesetting system (and the LaTeX extensions) installed on your computer to go directly to PDF. TeX is great, but it’s also quite big. And if you’re only producing the occasional PDF, then it’s overkill to have even a basic TeX system installed on your computer.</p>
  6. <p>With version 2 of pandoc came a new option: <em>–pdf-engine</em>. That option lets you specify which tool to use when doing a conversion to PDF. While you can still use LaTeX (and a few other tools) to do the deed, you don’t need to bother with all that bulk.</p>
  7. <p>Let’s take a look at how to create a PDF from a <a href="https://gum.co/learnmarkdown" target="_blank">Markdown</a> file using pandoc and a couple of lighter-weight utilities.</p>
  8. <h2 id="getting-started">Getting Started</h2>
  9. <p>You’ll need pandoc installed on your computer, along with one of these tools:</p>
  10. <p>For our purposes, WeasyPrint and wkhtmltopdf are essentially the same, but there are a couple or three little differences. I’ll touch on one of those differences in a moment.</p>
  11. <h2 id="doing-the-conversion">Doing the Conversion</h2>
  12. <p>As I mentioned a few paragraphs ago, I’m going to look at how to create a PDF from a file formatted with Markdown. To do that, open a terminal window on your computer. Navigate to the folder containing the file that you want to convert to PDF and then type either:</p>
  13. <p><code>pandoc [file-name].md --pdf-engine=weasyprint -o [file-name].pdf</code></p>
  14. <p>or</p>
  15. <p><code>pandoc [file-name].md --pdf-engine=wkhtmltopdf -o [file-name].pdf</code></p>
  16. <p>Where <em>[file-name].md</em> is the name of the Markdown file that you want to convert.</p>
  17. <p><strong>Note:</strong> Some people add the <em>-t html</em> option to those commands to create an HTML file that WeasyPrint and wkhtmltopdf then convert to PDF. The command also works without that option.</p>
  18. <p>Here’s the result of a conversion using wkhtmltopdf:</p>
  19. <figure>
  20. <img src="https://plaintextproject.online/images/pdf/wkhtmltopdf-conversion.png" alt="Markdown file converted to PDF with wkhtmltopdf">
  21. </figure>
  22. <p>What you get looks pretty much the same when you use WeasyPrint to do the conversion. The result is functional, but it’s not the nice PDF file that I promised you in the title of this article. So let’s look at how to add a bit of visual flair to a PDF generated with pandoc.</p>
  23. <h2 id="enter-print-css">Enter Print CSS</h2>
  24. <p><a href="https://en.wikipedia.org/wiki/CSS" target="_blank">Cascading Style Sheets</a> (CSS for short) is a way to change the look and feel of a web page or website. Print CSS extends that to formatting a web page or website to be printed. It adds support for changing page sizes, adding page breaks, hiding elements that shouldn’t be printed, adding a cover page, and more.</p>
  25. <p>I’m not going to into print CSS in any detail here, because it’s a big topic that my little brain has trouble wrapping itself around. However, I’m not going to leave you hanging. Here are some sources of information that can help you get started with print CSS:</p>
  26. <h2 id="using-print-css-in-a-conversion">Using Print CSS in a Conversion</h2>
  27. <p>Let’s say you’ve crafted your own print CSS file or begged/borrowed one from somewhere. To use it, you’ll need to add the <em>–css=</em> option to the string of options that you use with pandoc.</p>
  28. <p>So, once again, open a terminal window on your computer. Navigate to the folder containing the file that you want to convert to PDF and then type either:</p>
  29. <p><code>pandoc [file-name].md --pdf-engine=weasyprint --css=pdf-styles.css -o [file-name].pdf</code></p>
  30. <p>or</p>
  31. <p><code>pandoc [file-name].md --pdf-engine=wkhtmltopdf --css=pdf-styles.css -V papersize:a5 -o [file-name].pdf</code></p>
  32. <p>Change <em>pdf-styles.css</em> to the name of your print CSS file. Here’s an example the output, via WeasyPrint, from a print CSS file that I use:</p>
  33. <figure>
  34. <img src="https://plaintextproject.online/images/pdf/weasyprint-conversion.png" alt="Markdown file converted to PDF with WeasyPrint">
  35. </figure>
  36. <p>My stylesheet, while fairly basic:</p>
  37. <ul>
  38. <li>Sets the size of the page to <a href="https://papersizes.io/a/a5" target="_blank">A5</a>.</li>
  39. <li>Adds page breaks before every Heading 1.</li>
  40. <li>Creates margins of 1 inch (2.5 cm) all around.</li>
  41. <li>Uses the Overpass font for all text.</li>
  42. </ul>
  43. <p>The first point in the list above is one area in which WeasyPrint and wkhtmltopdf differ. wkhtmltopdf ignores page sizes that you specify in the CSS file. Instead, you need to specify the page size in the conversion command using the option <em>-V papersize:a5</em>.</p>
  44. <p>The examples I’ve included in this article are fairly basic. But, I hope, they’ll help spark a few ideas for converting your documents formatted with Markdown (or another markup language) to PDF using pandoc.</p>