-- Leo's gemini proxy

-- Connecting to alexschroeder.ch:1965...

-- Connected

-- Sending request

-- Meta line: 20 text/gemini

2024-03-27 Convert text file to PDF


I just couldn't find a good way to turn a text file into a PDF. Within Emacs, `ps-print-buffer` is hard to configure and my two specific problems were the following:


everything is turned into Latin-1 so I cannot use the EN dash;

the line length was set too short so that I couldn't print lines longer than 80 characters even though there was plenty of space left.


I wasted some time trying to write something in Bash, turned to Perl and wasted some more time trying to figure out why the encodings were all wrong except when the encodings were OK but Perl gave me a warning for every multi-byte character. So frustrating.


The solution ended up not being decoding and encoding the bytes in the Perl script but to use the `--encoding` option for `weasyprint`. In this case, it wasn't Perl's fault, it was my fear of Perl's encoding issues that led my down endless variations of `binmode`, `:utf8`, `decode_utf8` and `encode_utf8`. But I think I have it, now.


This is what I ended up with:


use `weasyprint`;

wrap it all in a `pre` tag.


I created a script called `to-pdf` with the following:


#!/usr/bin/env perl
use Modern::Perl;
use File::Basename;

if (@ARGV == 0) {
  die "Usage: to-pdf file.txt ...\n";
}

for (@ARGV) {
  open(my $text, "<", $_) or die "Cannot read $_\n";
  my ($name, $path, $suffix) = fileparse($_, ".txt");
  open(my $html, "|-") || exec "weasyprint", "--encoding", "utf-8", "-", "$path$name.pdf";

  print $html <<'BEG';
<!DOCTYPE html>
<body>
<pre style="font-family: Iosevka; font-size: 10pt">
BEG

  for (<$text>) { print $html $_ }; # copy text

  print $html <<'END';
</pre>
</body>
END
}

​#Administration #Printing #PDF ​#Printing #PDF ​#PDF


-- Response ended

-- Page fetched on Sat May 18 21:14:48 2024