[DOCKER] Output PDF with WeasyPrint

Introduction

Personally, wkhtmltopdf is familiar as a library for outputting PDF. However, I happened to find a library called WeasyPrint, so I tried it.

Installation

$ pip install weasyprint

Example of use

Convert the following HTML to PDF. Since it's a big deal, it can be displayed in two columns like a PDF document.

quanon/magica.html

The text is [Wikipedia »Puella Magi Madoka Magica](https://ja.wikipedia.org/wiki/%E9%AD%94%E6%B3%95%E5%B0%91%E5%A5%B3" Quoted from% E3% 81% BE% E3% 81% A9% E3% 81% 8B% E2% 98% 86% E3% 83% 9E% E3% 82% AE% E3% 82% AB).

01.png

We also have a PDF-specific CSS called pdf.css.

pdf.css


@page {
  margin: 8.0rem 2.0rem;
  font-family: YuMincho, "Hiragino Mincho ProN", serif;

  @top-center {
    content: "Magical Girl Madoka ☆ Magica";
    vertical-align: bottom;
    font-size: 1.2rem;
    border-bottom: 0.1rem solid;
    margin-bottom: 1.2rem;
  }

  @bottom-right {
    content: counter(page) " / " counter(pages);
  }
}

Output the PDF with the following command.

$ weasyprint magica.html magica.pdf -s pdf.css

It was: tada:

Here is the output PDF.

スクリーンショット 2017-09-11 22.46.36.png

It's easy to use and has a lot of documentation, so I'd like to continue using it: sparkling_heart:

Bonus: Docker compatible

Dockerfile


FROM python:3.6.2

RUN apt-get update \
 && apt-get install -y \
      python-lxml \
      fontconfig \
      libcairo2 \
      libpango1.0-0 \
      libgdk-pixbuf2.0-0 \
      libffi-dev \
      shared-mime-info \
      unzip \
 && apt-get autoremove \
 && apt-get clean

WORKDIR /opt

ENV WEASYPRINT_VERSION 0.40
RUN pip install weasyprint==$WEASYPRINT_VERSION

ADD https://noto-website.storage.googleapis.com/pkgs/NotoSerifCJKjp-hinted.zip .
RUN unzip -d noto NotoSerifCJKjp-hinted.zip \
 && mkdir -p /usr/share/fonts/opentype \
 && mv -fv noto /usr/share/fonts/opentype/noto \
 && rm -rfv NotoSerifCJKjp-hinted.zip \
 && fc-cache -fv

docker-compose.yml


version: '3.3'
services:
  weasyprint:
    build: .
    volumes:
      - .:/opt/data-volume
    working_dir: /opt/data-volume
    entrypoint:
      - weasyprint
    command:
      - -h
$ docker-compose run weasyprint magica.html magica.pdf -s pdf.css

reference

WeasyPrint

CSS

Recommended Posts

Output PDF with WeasyPrint
Output PDF with Django
PDF output with Latex extension in Sphinx
Markdown output with Django
View PDF with fbterm
Try Python output with Haxe 3.2
Test standard output with Pytest
Output to syslog with Loguru
Output large log with discord.py
Integrate PDF files with Python
How to output a document in pdf format with Sphinx
Number chapters and sections when PDF output (rst2pdf) with Sphinx
Output to csv file with Python
Input / output with Python (Python learning memo ⑤)
Output cell to file with Colaboratory
Output the call graph with PyCallGraph
[Note] Hello world output with python
Unit test log output with python
Convert PDF to image with ImageMagick
Try to output audio with M5STACK
Output networkX graph with graphviz (PyGraphviz)
Output color characters to pretty with python
Convert from PDF to CSV with pdfplumber
Extract Japanese text from PDF with PDFMiner
Simplify PDF password unlock with python + bat
UnicodeEncodeError struggle with standard output of python3
Convert PDF files to PNG files with GIMP
Rich cell output with Jupyter Notebook (IPython)