I want to store a binary file (pdf) in a document. To do this, I encode it in base64 (utf-8) and save it as an external file. My example doesn't work. If you use a text file instead of a pdf, you can see that extra lines appear when decoding. Encoded with this - https://base64.guru/converter/encode/text . How to fix it?


% !TeX program = lualatex
\setminted{xleftmargin=0.5cm, linenos, numbersep=5pt, breaklines, frame=single, framesep=1ex,  fontsize=\small}
io.output("base64.tex","w +");
%   \includegraphics[width=1.0\linewidth]{base64.pdf}

enter image description here

  • the base 64 block has windows ^M^J line ends exept the last line and has cyrillic comments % пакет для вывода листингов not shown in your image? Commented Jan 5, 2023 at 20:40
  • Your base64 text string contains \r\n sequences when decoded, this is a Windows-style line ending that may be displayed as two newlines in other systems. I think that is a separate issue from the pdf inclusion, as it seems to be a setting of the conversion website that you have used.
    – Marijn
    Commented Jan 5, 2023 at 20:41
  • you probably need wb not w mode so it does not mess with line ends Commented Jan 5, 2023 at 20:45
  • It works for a text file if you encode it here: appdevtools.com/base64-encoder-decoder . But I need to store a pdf. Encoding in Windows can be done like this: certutil -encodehex -f MTUCIfig.pdf base64.pdf 0x40000001 . There is the same problem with extra lines. Thanks for the help @David Carlisle
    – Youra_P
    Commented Jan 6, 2023 at 8:57
  • I found the documentation, but I'm doing something wrong. There is about what you said. w3.impa.br/~diego/software/luasocket/mime.html#unb64 The example doesn't work. Thanks for the help @Marijn
    – Youra_P
    Commented Jan 6, 2023 at 9:02

1 Answer 1


Everything works if you do it this way:

 local file = io.open("base64.txt", "wb");

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .