17

When you select "Print to File" in a Gnome print dialog (not cups-pdf):

print-to-file

... what is the actual command that gets executed? (I'm interested in the postscript variant, as on the image)

On the other hand - maybe there isn't a command call; maybe it is gtk_print_operation_set_export_filename from The new Gtk+ Printing API?

In any case - is there a way for me to utilize this particular code from the command line (not pdf2ps and the like)? Say, by using lpr to "print to file" to a postscript file - as if I was using the dialog above?

Renan
  • 16,976
  • 8
  • 69
  • 88
sdaau
  • 6,668
  • 12
  • 57
  • 69
  • 4
    AFAIK, this is a feature of the subsystem used by some GNOME and GTK+ applications to print stuff (some of these use Cairo). `lpr` interacts with `cups` directly, the print to file option completely bypasses the printing server. it probably doesn't call a command, it just invokes the cairo renderering function in a different way so it generates pdf, postscript or svg output to a file. – njsg Jun 07 '12 at 21:27
  • Ah, so its the cairo backend there! Any ideas if a comparable command line is available, that can invoke this rendering function in the same way as the print dialog does? Many thanks for the comment, @njsg - cheers! – sdaau Jun 07 '12 at 22:39
  • 4
    There may be some cairo command line tool (I really don't know), but I guess it's a bit tricky, as many applications use cairo to print their own documents, and that application is the one which is able to *understand* the file format. Cairo won't be able to read random file formats. The closest you can get is to check if the program with the dialog has some commandline option to print a file without opening the GUI. – njsg Jun 07 '12 at 23:15
  • Thanks for explaining that @njsg - the above screenshot was taken from the `evince` print dialog; unfortunately, I cannot see any command line switches by `evince` that would do something like this.. Cheers! – sdaau Jun 07 '12 at 23:18
  • This is just an UI convention, each program can in principle do what they wish when "Print to file" is selected. – vonbrand Jan 15 '13 at 15:18
  • 2
    It's called [`GtkPrintUnixDialog`](https://developer.gnome.org/gtk3/stable/GtkPrintUnixDialog.html). If you want to implement your own tool that launches that dialog here's an example in [`pygobject`](https://git.gnome.org/browse/pygobject/tree/demos/gtk-demo/demos/printing.py?id=3.13.3). I'm posting this as a comment because as far as I know there is no command that does what you want (so there's no answer to your question). The print dialog runs a `GtkPrintJob` (see documentation) which may use a `cairo`+`postscript` backend to get the surface and print the document. – don_crissti Oct 30 '16 at 17:51

5 Answers5

5

You could use the cups-pdf package as a solution independent from gnome. After installing the package, add the PDF printer using the cups webinterface at http://localhost:631 as described here ('General' as printer manufacturer, CUPS-PDF as driver).

For your convenience, change the output directory with the option Out in /etc/cups/cups-pdf.conf to, like, ${HOME} or whatever you want. Then do as suggested by @Noam to print to file.

J. Katzwinkel
  • 349
  • 3
  • 5
  • In Ubuntu 18.04, the required `apt` package is `printer-driver-cups-pdf`. It sets up a printer called "PDF". Then `lpr -P PDF file_to_print` works. The default location for files is `${HOME}/PDF`. – Klimaat Feb 22 '21 at 20:13
3

Print-to-file sends the document not to the printer but to a pdf document.

Same can be done with the cups printing system, which your applications use when they send print commands.

Use the cupsfilter tool. It effectively converts a document. It converts to pdf by default.

$ cupsfilter inputfilename > outputfilename.pdf

As described here.

markling
  • 183
  • 16
3
lpr -P "Print_to_PDF" file_to_print

To get the list of currently configured printers, run lpstat -p -d.

Noam Kremen
  • 184
  • 4
0

You can also use lp printing command to print a file to the default (or only) printer on the system:

lp filename

or to a specific printer

lp -d printer filename
0

In case anyone else is late to the party like me it seems the following does the trick at reproducing "print to file"

pdftocairo input.pdf output.pdf -pdf

I discovered this by opening a pdf with Evince in Gnome and then printing to file. It seems cairo graphics is used in the background when printing to a file this way: running pdfinfo on the resulting file reveals

Producer: cairo 1.16.0 (https://cairographics.org)