# Compile LaTeX to base64 PDF via command line

In my web application, clients are served with base-64 encoded PDFs. These PDFs are generated on the fly from LaTeX. Right now, they are compiled via pdflatex to files on the webserver's file system, read from their, encoded as base64 strings and sent to the client.

For various reasons, that doesn't seem the best solution. So: Is it possible to compile LaTeX directly to a base64 string (or anything which is convertible/"pipeable" to base64) without take the "file system detour"?

Of course, I'd need a command line solution (or even better: a native Node.js library which is not just a wrapper for pdflatex. Well, a man can dream...)

Not Base64, but plain HEX-encoding the PDF output is possible using PdfTeX's \pdffiledump built-in. Unfortunately, there is no Base64 filedump available in PdfTeX, but perhaps someone could program one in Lua for use with LuaTeX?

The example below writes the hex encoded output to the terminal and into the file \jobname.pdf.hex. Before piping the terminal output to somewhere, the normal pdftex output must be stripped, because it cannot be suppressed. The lines

#### \jobname.pdf.hex begin ####


and

#### \jobname.pdf.hex end ####


written on the terminal (STDOUT) may help you the write a filter for stripping.

To restore the PDF, run the xxd commandline tool on the HEX output:

xxd -r -p myDocument.pdf.hex  myDocument.pdf.hex.pdf


Example (runpdflatex at least twice):

\documentclass{article}
\usepackage{ifthen} %\whiledo

\edef\fileName{\jobname.pdf}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% writes hex encoded \jobname.pdf to the terminal and
% into the file \jobname.pdf.hex
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\IfFileExists{\fileName}{
\typeout{#### \jobname.pdf.hex begin ####}%
\newwrite\OutFile%
\immediate\openout\OutFile=\fileName.hex%
\def\fileoffset{0}%
\edef\filesize{\pdffilesize{\fileName}}%
\whiledo{\fileoffset<\filesize}{%
\edef\ouputLine{\pdffiledump offset \fileoffset\space length 30 {\fileName}}%
\immediate\write\OutFile{\ouputLine}%
\typeout{\ouputLine}%
\edef\fileoffset{\the\numexpr\fileoffset+30\relax}%
}%
\immediate\closeout\OutFile%
\typeout{#### \jobname.pdf.hex end ####}%
}{}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

\usepackage{kantlipsum}

\begin{document}
\kant[1-20]
\end{document}