Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can I print html files (with images, css) from the command-line?

I want to print styled html pages with their images from a script. Can anyone suggest an open-source solution?

I'm using linux (Ubuntu 8.04) but would be also be interested in solutions for other operating systems.

like image 491
Ken Avatar asked Nov 13 '08 09:11

Ken


People also ask

How do you print using CSS?

Developer Tools. The DevTools ( F12 or Cmd/Ctrl + Shift + I ) can emulate print styles, although page breaks won't be shown. In Chrome, open the Developer Tools and select More Tools, then Rendering from the three-dot icon menu at the top right. Change the Emulate CSS Media to print at the bottom of that panel.

Can HTML files be printed?

Open a document in the HTML editor. On the main menu, click File > Print Setup. The Print Setup dialog box appears. Specify the printer, printer properties, paper size, paper source, and orientation, then click OK to accept, or Cancel to abort changes.

What is @media print in HTML?

This rule allows you to specify different style for different media. So, you can define different rules for screen and a printer. The example below specifies different font families for screen and print. The next CSS uses the same font size for both screen as well as printer.


1 Answers

You could give html2ps a try, it is written in Perl, so I guess it wil run on any operating system that runs Perl. It does support CSS and images. It does not render as good as you may perhaps want.

To use in Debian/Ubuntu sudo aptitude install html2ps and then pipe the output to lpr to print:

html2ps \
    http://stackoverflow.com/questions/286583 \
    |lpr

Or pipe the output to ps2pdf to convert to a pdf file:

html2ps \
    http://stackoverflow.com/questions/286583 \
    |ps2pdf - stackoverflow.pdf
like image 96
Tader Avatar answered Sep 26 '22 18:09

Tader