0

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?

MWE:

% !TeX program = lualatex
%=========================
\documentclass{article}
\usepackage{graphicx}
\usepackage{xcolor}
\usepackage[newfloat]{minted}
\usemintedstyle{vs}
\setminted{xleftmargin=0.5cm, linenos, numbersep=5pt, breaklines, frame=single, framesep=1ex,  fontsize=\small}
\directlua{%
io.output("base64.tex","w +");
io.write((mime.unb64("JSAhVGVYIHByb2dyYW0gPSBsdWFsYXRleA0KJT09PT09PT09PT09PT09PT09PT09PT09PT0NClxkb2N1bWVudGNsYXNze2FydGljbGV9DQpcdXNlcGFja2FnZXtncmFwaGljeH0NClx1c2VwYWNrYWdle3hjb2xvcn0NClx1c2VwYWNrYWdlW25ld2Zsb2F0XXttaW50ZWR9DQpcdXNlbWludGVkc3R5bGV7dnN9DQpcc2V0bWludGVke3hsZWZ0bWFyZ2luPTAuNWNtLCBsaW5lbm9zLCBudW1iZXJzZXA9NXB0LCBicmVha2xpbmVzLCBmcmFtZT1zaW5nbGUsIGZyYW1lc2VwPTFleCwgIGZvbnRzaXplPVxzbWFsbH0NClxkaXJlY3RsdWF7JQ0KaW8ub3V0cHV0KCJiYXNlNjQudGV4IiwidyArIik7DQppby53cml0ZSgobWltZS51bmI2NCgiIikpKTt9DQpcYmVnaW57ZG9jdW1lbnR9DQolCVxpbmNsdWRlZ3JhcGhpY3Nbd2lkdGg9MS4wXGxpbmV3aWR0aF17YmFzZTY0LnBkZn0NClxpbnB1dG1pbnRlZHtURVh9e2Jhc2U2NC50ZXh9DQpcZW5ke2RvY3VtZW50fQ==")));}
\begin{document}
%   \includegraphics[width=1.0\linewidth]{base64.pdf}
\inputminted{TEX}{base64.tex}
\end{document}

enter image description here

11
  • 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

0

Everything works if you do it this way:

\directlua{%
 local file = io.open("base64.txt", "wb");
 file:write((mime.unb64("U3RyaW5nLTENClN0cmluZy0y")));
 file:close();
}

You must log in to answer this question.

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