Personally, wkhtmltopdf is familiar as a library for outputting PDF. However, I happened to find a library called WeasyPrint, so I tried it.
$ pip install weasyprint
Convert the following HTML to PDF. Since it's a big deal, it can be displayed in two columns like a PDF document.
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).
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.
It's easy to use and has a lot of documentation, so I'd like to continue using it: sparkling_heart:
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
WeasyPrint
CSS
Recommended Posts