Print html file with CUPS

vonPetrushev picture vonPetrushev · Nov 30, 2011 · Viewed 7.7k times · Source

Is there a way to explicitly tell the CUPS server that the file you are sending is text/html thus overriding the mime.types lookup?

Answer

Kurt Pfeifle picture Kurt Pfeifle · Feb 11, 2012

Yes, there is.

Use this commandline:

lp -d printername -o document-format=text/html file.html

Update (in response to comments)

I provided an exact answer to the OP's question.

However, this (alone) does not guarantee that the file will be successfully printed. To achieve that, CUPS needs a filter which can process the input of MIME type text/html.

Such a filter is not provided by CUPS itself. However, it is easy to plug your own filter into the CUPS filtering system, and some Linux distributions ship such a filter capable to consume HTML files and convert them to a printable format.

You can check what happens in such a situation on your system. The cupsfilter command is a helper utility to run available/installed CUPS filters without the need to do actual printing through the CUPS daemon:

touch 1.html
/usr/sbin/cupsfilter --list-filters 1.html

Now on a system with no HTML consuming filter ready, you'd get this response:

cupsfilter: No filter to convert from text/html to application/pdf.

On a different system (like on a Mac), you'll see this:

xhtmltopdf

You can even force input and output MIME types to see which filters CUPS would run automatically when asked to print this file an a printer supporting that particular output MIME type (-i sets the input MIME type, -m the output):

/usr/sbin/cupsfilter                  \
            -i text/html              \
            -m application/postscript \
            --list-filters            \
              1.html
  xhtmltopdf
  cgpdftops

Here it would first convert HTML to PDF using xhtmltopdf, then transform the resulting PDF to PostScript using cgpdftops.

If you skip the --list-filters parameter, the command would actually even go ahead and do the conversion by actively running (not just listing) the two filters and emit the result to <stdout>.

You could write your own CUPS filter based on a Shell script. The only other ingredient you need is a command line tool, such as htmldoc or wkhtmltopdf, which is able to process HTML input and produce some format that in turn could be consumed by the CUPS filtering chain further down the road.

Be aware, that some (especially JavaScript-heavy) HTML files cannot be successfully processed by simple command line tools into print-ready formats.

If you need more details about this, just ask another question...