From 89d46fdcc83d4607110389b0f6a69b039d9284ff Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andr=C3=A9as=20Livet?= Date: Tue, 17 Jan 2023 14:12:46 +0100 Subject: [PATCH] Ajout de make pdf avec weasyprint --- Makefile | 12 +++++++++ pdftoc-to-latex | 71 +++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 83 insertions(+) create mode 100755 pdftoc-to-latex diff --git a/Makefile b/Makefile index d0c3cbf..222f524 100644 --- a/Makefile +++ b/Makefile @@ -18,3 +18,15 @@ help: # "make mode" option. $(O) is meant as a shortcut for $(SPHINXOPTS). %: Makefile @$(SPHINXBUILD) -M $@ "$(SOURCEDIR)" "$(BUILDDIR)" $(SPHINXOPTS) $(O) + +# Default make weasyprint target has a bug so we have to call it manually on the generated index.html file +# After that, extract table of content data from the pdf and generate a .tex file +# Then generate a toc.pdf from the .tex file +# And put them together in a .pdf file +# After all, clean up +pdf: weasyprint + weasyprint build/weasyprint/index.html build/weasyprint/index.pdf -s source/css/print-theme.css + ./pdftoc-to-latex build/weasyprint/index.pdf > build/weasyprint/toc.tex + - pdflatex -interaction nonstopmode -output-directory=build/weasyprint build/weasyprint/toc.tex + pdftk A=build/weasyprint/index.pdf B=build/weasyprint/toc.pdf cat A1 B A2-end output build/weasyprint/vheliotech.pdf + rm build/weasyprint/index.pdf build/weasyprint/toc.tex build/weasyprint/toc.pdf build/weasyprint/GuidedemontageVheliotech.pdf \ No newline at end of file diff --git a/pdftoc-to-latex b/pdftoc-to-latex new file mode 100755 index 0000000..5f475b9 --- /dev/null +++ b/pdftoc-to-latex @@ -0,0 +1,71 @@ +#!/usr/bin/perl + +# Script taken from https://gitlab.com/derobert/random-toys/blob/master/pdf/pdftoc-to-latex +# Manually adapt for sphinxdoc generated pdf +use 5.024; +use strict; +use warnings qw(all); +use IPC::Run3; +use LaTeX::Encode; +use Encode qw(decode); + +my @levels + = qw(chapter section subsection subsubsection paragraph subparagraph); +my @counters; + +my ($data_enc, $data); +run3 ['pdftk', $ARGV[0], 'dump_data_utf8'], undef, \$data_enc; +$data = decode('UTF-8', $data_enc, Encode::FB_CROAK); + +my @latex_bm; + +my $bm; +foreach (split(/\n/, $data)) { + /^Bookmark/ or next; + if (/^BookmarkBegin$/) { + add_latex_bm($bm) if $bm; + $bm = {}; + } elsif (/^BookmarkLevel: (\d+)$/a) { + ++$counters[$1 - 1]; + $#counters = $1 - 1; + $bm->{number} = join(q{.}, @counters); + $bm->{level} = $1 - 1; + } elsif (/^BookmarkTitle: (.+)$/) { + # In Sphinx title include a utf-8 icon of a link, so remove it + my $title = substr($1, 0, -1); + $bm->{title} = latex_encode($title); + } elsif (/^BookmarkPageNumber: (\d+)$/a) { + $bm->{page} = $1; + } else { + die "Unknown Bookmark tag in $_\n"; + } +} +add_latex_bm($bm) if $bm; + +print < 2 + if ($bm->{level} == 0 || $bm->{level} > 2) { + return; + } + # Make sections become chapters + my $level = $levels[$bm->{level} - 1]; + # Remove the first number because it refers to the root title (ie always 1.X) + my $number = substr($bm->{number}, 2); + my $title = $bm->{title}; + my $page = $bm->{page}; + + push @latex_bm, <