Control everything via your keyboard!

Latest version:

Command Line Print 0.5.2

Platform: All

Mozilla Firefox 1.5 - 3.6

Last Update: 2010-01-26

Note for download

sites.google.com does not allow me to upload a file whose file extension is ".xpi". So this file is now zero-compress zipped. I'm sorry for the inconvenience.

Usage:

>firefox -print http://www.example.com/index.html
>firefox -print http://www.example.com/index.html -printmode pdf -printfile ../foo.pdf
>firefox -print http://www.example.com/index.html -printmode PNG
>firefox -print http://www.example.com/index.html -printdelay 10

This application tries to reproduce the last printing situation. When you like to change settings such as page size, all you need to do is to print a page from normal browsing window i.e. File -> Print.... You can also set margins and other formats, through File -> Page Setup... and File -> Print Preview.

-print <URL>
@required. If this option is not specified, the application ignores rest of options, if any, and a normal browser launches.
-printmode mode
@optional. mode argument can be either {0, 1, 2, 3, printer, pdf, png, ps, postscript}. Case-insensitive. If this option is not specified, this application behaves according to user preference settings. See also: Tools -> Add-ons -> Extensions -> Command Line Print -> Options.
  • Printer should work with Firefox 1.5~. @default on initial installation.
  • PDF requires Firefox version is 3.0 or later.
  • PNG requires Firefox version is 2.0 or later.
  • PS or PostScript requires GTK toolkit and build option --enable-postscript, so that among official builds: Windows, Mac OSX and Linux, it works only with Linux build.
-printfile filepath
@optional.  filepath is an absolute, or a relative path from the current directory, like other CLI applications. Any local path will do, as long as Gecko understands where. This option will be ignored, if mode argument is Printer. If this option is not specified, a new file will be saved at default location: download directory, where you can set Tools -> Options... -> Main -> Downloads.
-printdelay seconds
@optional. This option sets the duration between loading document and firing printing event. Normal users don't have to use this option, as it just forces the application to put off starting a print job. seconds is an integer from 0 to 120.
Gecko, as a layout engine, is able to detect an end of document loading, but after that, some AJAX site may go on another data fetch and re-rendering itself by JavaScript. In theory, no engine can determine when such a website becomes its final appearance. In practice, we expect web developers have designed it soon enough not to have a visitor click browser's back button. So a few seconds would be a proper value. At most, one minutes or so. Elapsed time for downloading or rendering is already taken into account. However large in size a site a user may try to print out, he/she doesn't have to set this option, unless it uses AJAX technology.

For Unix users:

Firefox uses realpath(3) in <stdlib.h>, to resolve a relative path. In case the target does not exist, it just throws ENOENT and stops silently.

Bad example:

>firefox -print http://www.example.com/ -printfile foo.png

Good example A:

>firefox -print http://www.example.com/ -printfile ~/foo.png

Good example B:

>touch foo.png
>firefox -print http://www.example.com/ -printfile foo.png

See also:

  1. Linux Programmer's Manual REALPATH(3)
    http://www.kernel.org/doc/man-pages/online/pages/man3/realpath.3.html
  2. BSD Library Functions Manual REALPATH(3)
    http://developer.apple.com/documentation/Darwin/Reference/ManPages/man3/realpath.3.html
  3. touch: Change file timestamps
    http://www.gnu.org/software/coreutils/manual/html_node/touch-invocation.html
    Any file argument that does not exist is created empty.

For Mac OSX users:

Firefox on Mac OSX is not able to generate PDF, for its development was abandoned years ago.

Note for advanced users:

This subsection is a brief summary of conversations between users and me, so far.

Multiple printers

In case you own several printers, you may like to switch which to send print job to by command line. First of all, you need to create Firefox profiles as many as printers, namely, printer1, printer2, printer3... Then, you can switch those profiles with -p option so in each profile you need to configure print settings. Maybe your command will be called within a very short period, i.e. during another priting job, so that you can turn off the remote mode by setting a MOZ_NO_REMOTE=1 environment variable, or a new command line option -no-remote.

>firefox -no-remote -p printer1 -print http://www.example.com/index.html

However, if you turned off remote mode, profile locking (a lock file in a profile directory) popups GUI dialog, when you are going to use the same profile simultaneously. It's hard to decide which is better.

Finally I changed my mind, because I came to conclusion that switching profile is a troublesome and ineffective solution. Then I added a new option -printprinter. If this flag seems something unreadable you can type it -PrintPrinter instead.

-printprinter printername
@optional. If this option is not specified, this application chooses the default printer in the system. If mode argument is Printer, this application checks whether printername argument matches any of registered printer names. Otherwise, any argument is valid even if it does not exist. In case you have no idea on what names your printers are, please take a look at about:config and type "print" in the filter box.
>firefox -print www.example.com -printprinter "Microsoft Office Document Image Writer"


See also:

  1. Managing profiles
    http://support.mozilla.com/en-US/kb/Managing+Profiles
  2. Running two instances of Mozilla simultaneously
    https://developer.mozilla.org/en/Debugging_Mozilla_on_Windows_FAQ#Running_two_instances_of_Mozilla_simultaneously
  3. About:config
    http://kb.mozillazine.org/About:config

Yet Another reason to use different profiles

Drupal France, a local CMS community, introduces this application, as a way to convert their web contents into a PDF file.

It's a very good hack, but there's a small problem: If I update this application to a new version, say, due to fixing a typo in comment lines, users need to re-hack the scripts again. As you see in "Old versions" section, the risk of update is not so high, but anyway you need to re-install the extension when Firefox itself bumps the major version.

To avoid inventing several wheels, you can specify printing settings without GUI, via user preferecne (user.js or prefs.js). Below is an example of user.js, which is equivalent to the hack "Utilisation de Firefox/Gecko" by drupalfr.

/* To omit typing "-printmode PDF" */
user_pref("extensions.cmdlnprint.mode", 1);

/* fake printer "drupal pdf dummy" */
user_pref("print.printer_drupal_pdf_dummy.print_downloadfonts", true);
user_pref("print.printer_drupal_pdf_dummy.print_edge_bottom", 0);
user_pref("print.printer_drupal_pdf_dummy.print_edge_left", 0);
user_pref("print.printer_drupal_pdf_dummy.print_edge_right", 0);
user_pref("print.printer_drupal_pdf_dummy.print_edge_top", 0);
user_pref("print.printer_drupal_pdf_dummy.print_in_color", true);

user_pref("print.printer_drupal_pdf_dummy.print_bgcolor", true);
user_pref("print.printer_drupal_pdf_dummy.print_bgimages", true);

/* Note that margins are string, while unwritable margins are integer. */
user_pref("print.printer_drupal_pdf_dummy.print_margin_top", "0.5");
user_pref("print.printer_drupal_pdf_dummy.print_margin_bottom", "0.5");
user_pref("print.printer_drupal_pdf_dummy.print_margin_left", "0.5");
user_pref("print.printer_drupal_pdf_dummy.print_margin_right", "0.5");

user_pref("print.printer_drupal_pdf_dummy.print_unwritable_margin_top", 0);
user_pref("print.printer_drupal_pdf_dummy.print_unwritable_margin_bottom", 0);
user_pref("print.printer_drupal_pdf_dummy.print_unwritable_margin_left", 0);
user_pref("print.printer_drupal_pdf_dummy.print_unwritable_margin_right", 0);

user_pref("print.printer_drupal_pdf_dummy.print_headercenter", "");
user_pref("print.printer_drupal_pdf_dummy.print_headerleft", "&T");
user_pref("print.printer_drupal_pdf_dummy.print_headerright", "");

user_pref("print.printer_drupal_pdf_dummy.print_footercenter", "");
user_pref("print.printer_drupal_pdf_dummy.print_footerleft", "&U");
user_pref("print.printer_drupal_pdf_dummy.print_footerright", "");

/* paperWidth is also string, as it stands for float (or double). */
user_pref("print.printer_drupal_pdf_dummy.print_paper_width", "280");

user_pref("browser.sessionstore.resume_from_crash", false);

Commands to input:

>firefox -print artisan.karma-lab.net/print/1711 -printprinter "drupal pdf dummy"

See also:

  1. A Brief Guide to Mozilla Preferences
    https://developer.mozilla.org/En/A_Brief_Guide_to_Mozilla_Preferences
  2. Conversion HTML/CSS vers PDF pour Drupal (ou autre)
    http://artisan.karma-lab.net/print/1711
  3. nsPrintOptionsImpl.cpp
    http://mxr.mozilla.org/mozilla/source/widget/src/xpwidgets/nsPrintOptionsImpl.cpp

Installation onto another Gecko family

This application does not use Firefox specific features, other than download directory. So This can work with other members of Gecko family such as SeaMonkey, only with a simple modification to the install manifest. Maybe even with Thunderbird and XULRunner.

>thunderbird -print http://www.example.com/index.html
See also:
  1. Install Manifests
    https://developer.mozilla.org/en/Install_Manifests
  2. Valid Application Versions
    https://addons.mozilla.org/en-US/firefox/pages/appversions

Size of windows and generated Images

This application controls the window size as 800px-600px via an inline style attribute (CSS). If you are interested in the size of generated images, the size of an original window is an important factor. I believe users should always be able to close a GUI window and that is why the application window has a title bar. But some power users may using this application without a glance or without even an optical device. If you are really a CLI guru and, for example, are using virtual display to suppress popping-up GUI, it's worth trying to remove the title bar, and probably worth trying to make it render in full screen mode.
See also:

  1. Position and size features
    https://developer.mozilla.org/en/DOM/window.open#Position_and_size_features

When you have some troubles...

Feel free to send me your comment to torisugari at gmail.com. Or message board http://torisugari.hostei.com/

Page Modified: 2010-01-28

Old versions:

2009-04-09 Command Line Print 0.5.1

2009-03-29 Command Line Print 0.5.0

2009-03-29 Command Line Print 0.4.5

2009-03-27 Command Line Print 0.4.4

2008-07-14 Command Line Print 0.4.3

2008-07-09 Command Line Print 0.4.2

2008-06-05 Command Line Print 0.4.1

2007-03-21 Command Line Print 0.2.2 

2007-03-09 Command Line Print 0.2.1 

2006-11-03 Command Line Print 0.2.0

2006-11-XX Command Line Print 0.1.0