3
1

More than 3 years have passed since last update.

Hugo で つくる website の PDF バージョンをつくる

Posted at

動機

Hugo で documentationを用意しつつ、それを pdf としても読めるようにしたい

参考

https://discourse.gohugo.io/t/generate-hugo-website-as-a-pdf/22855/4
Nader_K_Radさんの投稿

やりたいこと

複数 page を ひとつの pdf として出力

方法

guide.pdfというものが出力pdfと想定
一時的に allというcontentを用意して、そこに 全pageをcombineしたhtmlを生成し、それをwkhtmltopdfを使って pdf化する。wkhtmltopdfは https://wkhtmltopdf.org/ で手に入れる。

#!/bin/sh

mkdir content/all
touch content/all/_index.md

hugo --cleanDestinationDir
wkhtmltopdf public/all/index.html --allow public public/downloads/guide.pdf

rm -r public/all
rm -r content/all

さて、all/index.htmlはどのようにしてbuild するのか? これは layoutで定義する。使っている theme の css などを使いつつ、layout定義する場合は、 /themes/your_theme/layoutsのなかにallというfolderを掘り、そこにlist.htmlというfileを用意します。

list.html
<!DOCTYPE html>
<html>
    {{- partial "head.html" . -}} <!-- depends on the theme -->
    <style>
      h1 {page-break-before: always;} <!-- if h1 is only used at the title of the pages, this will be used as page-break in the pdf. -->
    </style>
    <body>
        <div class="container-fluid"> <!-- my theme is based on bootstrap -->
            <div class="row">            
              {{ range .Site.Pages }}
              <div class="main col-12 order-1 col-md-9 col-lg-10 col-xl-10 py-3">
                <h1 id="{{ .RelPermalink }}">{{ .Title }}</h2>
                {{ .Content }}            
                </div>
              {{ end }}
            </div> <!-- /end of row -->
        </div> <!-- /end of container -->
    </body>
</html>

これで all/index.html にすべてのpage (range .Site.Pages) が書き出されるので、それを pdfにしている。

3
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
3
1