How can I print from Vim to PDF?

22,484

Solution 1

Vim will not show you the print dialog box. Instead, you can print to a PostScript file, open it in a PostScript viewer and print from there.

To print to a PostScript file from Vim:

:hardcopy > myfile.ps

You can also convert PostScript to PDF using ps2pdf:

ps2pdf myfile.ps

Solution 2

Building on what others have already stated:

You can use the following single-line Vim command to create a PDF file:

:hardcopy > %.ps | !ps2pdf %.ps && rm %.ps

Note:

  • The % is shorthand for the current filename, so HelloWorld.C will print to HelloWorld.C.pdf
  • If you want to also retain the intermediate .ps file, simply omit the && rm %.ps, obtaining: :hardcopy > %.ps | !ps2pdf %.ps

Additionally, to change the rendered font, set the printfont before executing the hardcopy command. For example, to select Courier 8:

:set printfont=Courier:h8

Putting it all together, I decided to add the following to my .vimrc file so that I can simply execute the :HardcopyPdf command. This command can also operate on a selected range within a file:

" Select the font for the hardcopy
set printfont=Courier:h8
command! -range=% HardcopyPdf <line1>,<line2> hardcopy > %.ps | !ps2pdf %.ps && rm %.ps && echo 'Created: %.pdf'
Share:
22,484

Related videos on Youtube

caligula
Author by

caligula

Updated on September 18, 2022

Comments

  • caligula
    caligula over 1 year

    I would have thought this is rather simple, but I don't get it done: I use gVim and would like to get the text as a PDF file. In other applications like Firefox the print dialog shows me available printers and I can choose to print directly to a PDF file. However, in Vim there is no such dialog and the file is just sent to the standard printer of the system.

    I tried the following:

    • I'm not able to make the "print-to-pdf" thing my standard way of printing via the printer window of Ubuntu.
    • :ha > file converts my file to a .ps file. That's nice, but .pdf would be nicer...
  • caligula
    caligula about 8 years
    The first proposal means just changing the file extension... The second one might work in a mapping or something like that, I will have to do some more research to get that done... Thanks so far...
  • Ron
    Ron about 8 years
    It is as easy as changing an extension ;)
  • Ben Kushigian
    Ben Kushigian almost 7 years
    This is awesome, definitely didn't know this was a thing! Can you get multiple files into a single document?
  • lucidbrot
    lucidbrot over 6 years
    (cygwin user) :ha > file.pdf did not create a valid pdf file. Combining it with ps2dpf did work but gives me white background. I'm happiest with :TOhtml file.html and then printing this html file to pdf
  • DrBeco
    DrBeco almost 5 years
    I used a variation of this command today: :1,50 hardcopy, it didn't create a file, just printed from lines 1 to 50.
  • arr_sea
    arr_sea over 4 years
    Additionally, to change the rendered font, I found I could do the following prior to the hardcopy command (to select Courier 8): :set printerfont=Courier:h8
  • theintz
    theintz over 3 years
    That is very neat Mathias, thanks. The range thing didn't work for me. I highlighted a range (using v) and the command acted on the whole file and ignored the range.