I’m using https://github.com/matcornic/hugo-theme-learn as a documentation website. I’m looking for a way to provide an offline version of the website as PDF. This would assume to be able to convert Markdown to PDF, but especially order page in the right way before converting the data.
I don’t believe there is a way to do this automatically with Hugo. There are some default arguments being passed to helpers like Pandoc, but last time I checked I couldn’t find out to change them:
Once those can be changed then you should be able to include PDF generation arguments for Pandoc.
There are couple alternative options.
The easiest would probably be to create a wrapper script around Pandoc that adds these arguments. Unless you can get help from someone else with this, I don’t have the time to invest in doing it for you.
PDF documents can be created from a custom output format template, but this is probably way more complicated than it is worth which is why it hasn’t been done.
Thank you for pointing me out at this issue. I’ll follow it!
I could write a script with pandoc and try to sort the markdown but I’m not sure I want to take this path…
I’ve done it recently, and this is how I’ve done it:
There’s a page in Hugo, with a different layout, that combines all pages and outputs an HTML that is suitable for PDF. Then, this is the [simplified] build script I run each time I want to update my website:
I’ve also excluded the source HTML page from the sitemap.
Most of the styles in the PDF file will come from the HTML file, but some that are specific to PDF, like margins, can be set using a YAML settings file for wkhtmltopdf.
I’ve also piped a few SED’s to the script above to adjust the HTML content for PDF conversion (e.g., replacing iframes with fixed images), but you can also do it in Hugo when setting up that page.
It’s a section ("/all-content/") with its own layout. This is a simplified version of the layout:
<!DOCTYPE html>
<html lang="en">
<head prefix="og: http://ogp.me/ns# fb: http://ogp.me/ns/fb# product: http://ogp.me/ns/product#">
<meta charset="utf-8">
<xbase href="file:///mnt/websites/xxxxxxxx/public">
<title>All the Posts!</title>
<style>
</style>
</head>
<body>
<h1>All the Posts!</h1>
<p>This is a copy of all posts on the website. To download the latest version, visit...</p>
<p>TOC:</p>
<ul>
{{ range .Site.RegularPages }}
<li><a href="#{{ substr .RelPermalink 1 -1 }}">{{ .Title }}</a></li>
{{ end }}
</ul>
{{ range .Site.RegularPages }}
<h2 id="{{ substr .RelPermalink 1 -1 }}">{{ .Title }}</h2>
<p style=""><a href="{{ .Permalink }}">{{ .Permalink }}</a> - {{ .Params.sdate }}</p>
{{ .Content }}
{{ end }}
</body>
</html>
I should warn you, however, that I’m not fully expert in Hugo; others in this forum may be able to give you much better solutions. This is just something I could come up, which works fine for me.
Hi there,
i’m working on Paged.js, an html to pdf solution that follows the w3c standards, to print books easily. Incidently, i made our new website using Hugo, and setup a paged.js exemple. Check the website i made using Hugo (for paged.js, the lib that let you print and make PDF from HTML : https://www.pagedjs.org
you got a button top right on this page to set up the preview. Once it’s done, you only need to print from the browser, and you’re all set. Browser side printing as it should be
one thing: the print preview button/link is not showing up on all the pages …
If you’re talking about pagedjs website, that’s what we wanted: if it makes sense to print single page, i’m not sure that printing list pages make sense. But there is no bug here
Create a new directory in the content directory. I’ve called it “all-content”.
Add a simple index.md file to that directory. The content is not important.
Right now, this new directory will use the default layout, which is not fine for our purpose. Create a directory with the same name in layouts; e.g., /layouts/all-content/. Now you can define a new layout for this page.
Create /layouts/all-content/list.html, with a layout that includes all pages on the website. Use my second reply to the original post above as a starting point and then refine it to match your preferences.
Create a build script to first build the website using Hugo, and then convert /public/all-content/index.html to pdf. Use my first reply above as a starting point and then adjust it to your need. You may need to install a few things such as wkhtmltopdf too.
Refine the rest of the website by excluding that page from the sitemap and RSS feed, etc.