PDF: fix image fonts

This commit is contained in:
Ben Edgington
2025-07-11 20:13:32 +01:00
parent 7ebc7392f1
commit a5fd539da0
116 changed files with 109 additions and 27 deletions

2
.gitignore vendored
View File

@@ -20,7 +20,7 @@ src/md/annotated.md
# Junk
tmp/
test*
*.pdf
/*.pdf
*.bkp
# IntelliJ configs

View File

@@ -38,6 +38,18 @@ The generated PDF will be written to your current directory as _book.pdf_.
- To get two-sided output, omit the `-variable classoption:oneside` parameter.
### Diagrams
I've had to wrestle a bit with diagram handling.
I am now using [drawio-desktop](https://github.com/jgraph/drawio-desktop) to create diagrams - this simplifies the workflow over using the web version. The script in _bin/util/drawio2image.sh_ can convert the _.drawio_ files to SVG or PDF.
Ideally we would like to do everything with SVGs alone, but the command-line version of drawio-desktop seems to be incapable of outputting SVGs that librsvg2 (used by pandoc) can render with the correct Google font. It is possible to export to SVG via the drawio GUI and embed the fonts, but that is cumbersome and results in huge files for the HTML version. (The background to the issue is that drawio uses `<foreignObject>` elements for text handling - web browsers understand this, but librsvg2 does not.)
Instead, we now maintain SVG versions (in _src/images/diagrams/_) for the HTML build, and PDF versions (in _src/images/diagrams\_pdf/_) for the PDF build. These are manually maintained by running the _drawio2image.sh_ utility as required - we could automate and cache and all that jazz, but for now that seems too much bother.
Note that, in the SVG for charts, all text is converted to paths, so they import to pandoc no problem. I miss being able to do that with drawio.
### Significant known issues
- Intermittent: sometimes pages with diagrams overflow off the bottom.

View File

@@ -31,29 +31,30 @@ cd $srcdir
echo "Converting $srcdir/$name.md to ./$name.pdf with version $UE_GIT_BRANCH"
pandoc \
$name.md \
--output $outdir/$name.pdf \
--from markdown \
--metadata title-meta:"Upgrading Ethereum - ${UE_GIT_BRANCH^} Edition" \
--metadata author-meta:'Ben Edgington' \
--metadata lang:en-GB \
--lua-filter $path/filters/pagebreaks.lua \
--lua-filter $path/filters/links.lua \
--lua-filter $path/filters/figures.lua \
--lua-filter $path/filters/summaries.lua \
--lua-filter $path/filters/linebreaks.lua \
--lua-filter $path/filters/codeblocks.lua \
--variable documentclass:book \
--variable classoption:oneside \
--variable linkcolor:violet \
--variable geometry:a4paper \
--variable geometry:margin=2.54cm \
--variable block-headings \
--variable monofont:'DejaVu Sans Mono' \
--include-before-body $path/inc/title-page.tex \
--include-in-header $path/inc/header.tex \
--toc \
--toc-depth 3 \
--no-highlight \
--pdf-engine xelatex
cat $name.md | \
sed 's|\(images/diagrams\)/\([^)]*\)\.svg|\1_pdf/\2.pdf|g' | \
pandoc \
--output $outdir/$name.pdf \
--from markdown \
--metadata title-meta:"Upgrading Ethereum - ${UE_GIT_BRANCH^} Edition" \
--metadata author-meta:'Ben Edgington' \
--metadata lang:en-GB \
--lua-filter $path/filters/pagebreaks.lua \
--lua-filter $path/filters/links.lua \
--lua-filter $path/filters/figures.lua \
--lua-filter $path/filters/summaries.lua \
--lua-filter $path/filters/linebreaks.lua \
--lua-filter $path/filters/codeblocks.lua \
--variable documentclass:book \
--variable classoption:oneside \
--variable linkcolor:violet \
--variable geometry:a4paper \
--variable geometry:margin=2.54cm \
--variable block-headings \
--variable monofont:'DejaVu Sans Mono' \
--include-before-body $path/inc/title-page.tex \
--include-in-header $path/inc/header.tex \
--toc \
--toc-depth 3 \
--no-highlight \
--pdf-engine xelatex

69
bin/util/drawio2image.sh Executable file
View File

@@ -0,0 +1,69 @@
#!/bin/bash
# Export drawio diagrams to SVG files
#
# A drawio file can have multiple diagrams/tabs/pages. We extract them individually and
# make an SVG filename that includes the drawio basename and the diagram's tab name.
# The SVG files are written to the current working directory.
# Note that (as of 27.0.9 anyway) this doesn't work, and neither does --disable-update
export DRAWIO_DISABLE_UPDATE=true
drawio=/usr/bin/drawio
if [ $# -lt 2 ]; then
echo "Usage: $0 <svg|pdf> <drawio_file> [tab_name]"
exit 1
fi
ext="$1"
if [ "$ext" = "svg" ]; then
drawio_opts="-b 10 --svg-theme light"
elif [ "$ext" = "pdf" ]; then
drawio_opts="-b 10 --crop"
else
echo "Error: specify svg or pdf output format"
exit 1
fi
drawio_file="$2"
if [[ $drawio_file != *.drawio ]]; then
echo "Error: input file must have a .drawio extension"
exit 1
fi
page=""
if [ $# -gt 2 ]; then
page="$3"
fi
errors=$(mktemp)
trap 'rm -f -- "$errors"' EXIT
root=$(basename -s '.drawio' $drawio_file)
names=$(grep -oP '<diagram[^>]*name="\K[^"]+' $drawio_file)
# If there is only one tab, we don't need to rename or page count
if [[ $(echo $names | wc -w) == 1 ]]; then
echo "$drawio_file -> $names.$ext"
$drawio $drawio_opts -x -o $names.$ext $drawio_file >/dev/null 2>$errors
if [ $? -ne 0 ]; then
cat $errors
exit 1
fi
exit 0
fi
# Loop over the diagrams and convert them individually
n=1
for name in $names; do
if [ "$page" != "" ] && [ "$page" != "$name" ]; then
continue
fi
echo "$drawio_file[$name] -> $root-$name.$ext"
$drawio $drawio_opts -x -p $n -o $root-$name.$ext $drawio_file >/dev/null 2>$errors
if [ $? -ne 0 ]; then
cat $errors
exit 1
fi
((n++))
done

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Some files were not shown because too many files have changed in this diff Show More