How to print a file from VsCode?

50,275

Solution 1

It is not yet available, but I found an issue report for that feature.

It's currently in the backlog, so we can expect it soon.

https://github.com/Microsoft/vscode/issues/5953

Edit: In the meantime:

Solution 2

Now you can install addon for VS Code - search "PrintCode".

Note that the repository for this extension was last updated in February 2018 with numerous issues outstanding.

Solution 3

I found the PrintCode extension inspiring but unfinished and with a number of flaws. In particular it depends on a specific paper size to wrap and as a result does not respond well if you change paper size or orientation in the print dialog.

It's open source so I looked at the code and didn't like that either. No programmer ever likes another's coding style. So I pinched the idea of using a web-browser as platform driver for HTML printing -- my hat is off to the PrintCode author for that cunning insight -- and wrote my own.

In the process I fixed all the known bugs, added everything on my wishlist and a couple of things suggested by others. The biggest thing was figuring out the CSS required to respect print dialog paper size and orientation. This also sorts out the mysterious disappearing line numbers problem, although I'm not sure why. The next biggest thing was learning to probe for an unused port, a problem that also afflicts PrintCode causing the browser to open showing no content.

Major issues

  • PrintCode depends on a web service. You can't use it offline.
  • Many people want to be able to open a file, select a portion and print just the selection.
  • The print dialog supports changing paper size and orientation. This clashes with the way PrintCode works.
  • Some people like to run multiple VS Code windows.
  • When you print a markdown file, you probably don't want it printed like a text file when it can be rendered with fonts and proper headings and bullets etc.
  • Support is required for remote workspaces.

Because each instance of VS Code needs a different port for its embedded webserver, you can't just use a setting. Dynamic port allocation is necessary.

Remote workspaces weren't even a thing until two years after maintenance ceased on PrintCode.

If you want to survey your options, get onto https://marketplace.visualstudio.com, choose the Visual Studio Code tab and search for printing.

If you just want a link to my version, it's here http://marketplace.visualstudio.com/items?itemName=pdconsec.vscode-print.

If you like what you see but need something I haven't thought of, the marketplace page has a link to the repo on github. Create an issue and tell me what you need -- or write it yourself and submit a PR.

Solution 4

Now there is an extension available for printing from the VS Code Editor. It's called VS Code Printing Free. I've tried it for a couple of days and it works fine.

Share:
50,275
Eric Bole-Feysot
Author by

Eric Bole-Feysot

Software developer c#. Romcenter rom manager author.

Updated on February 11, 2022

Comments

  • Eric Bole-Feysot
    Eric Bole-Feysot over 2 years

    I want to send the file I'm currently editing to a printer, like I did in Notepad++ for example. I havn't found any hint on a print command. Is it not possible ?

  • Zain Rizvi
    Zain Rizvi almost 6 years
    Another option: PolaCode
  • Angry 84
    Angry 84 over 5 years
    stock install and it using chrome my default browser produces a corrupt pdf from a php file that has php, htm and js. (only missing style/css tag)
  • Nat Kuhn
    Nat Kuhn about 5 years
    Hope you will put yours in the marketplace or find some other way to share it!
  • Peter Wone
    Peter Wone about 5 years
    I have put it in the marketplace. Direct linking to my own work seemed a bit close to spam. Since you are specifically asking for my version, here it is: marketplace.visualstudio.com/…
  • Peter Wone
    Peter Wone about 5 years
    @NatKuhn see comment above
  • Nat Kuhn
    Nat Kuhn about 5 years
    thank you! I had actually followed your instructions, but when I searched for print I found 12 extensions, none of which had your name on it. So your direct link was helpful, and now I know that searching for "vs code printing" also works.
  • Magnus Lind Oxlund
    Magnus Lind Oxlund about 5 years
    As long as you disclose your affiliation, it's perfectly fine to include a direct link in your answer.
  • Alim Özdemir
    Alim Özdemir almost 4 years
    As of 09/15/2020, 'soon' seems still far away. Instead the aforementioned extensions, pretty sad.
  • Jon-Eric
    Jon-Eric almost 3 years
    VS Code Printing Free by Peter Wone, i.e., the answer above this one.
  • SultanOrazbayev
    SultanOrazbayev over 2 years
    As of Nov 25, 2021, 'soon' is still far away.
  • Peter Wone
    Peter Wone over 2 years
    @Angry84 the problem you describe has nothing to do with the extension. By the time you save a PDF, rendering as HTML is complete and control has passed to your web browser.
  • AwesomeCronk
    AwesomeCronk about 2 years
    Three years on...
  • Peter Wone
    Peter Wone about 2 years
    I've shortened the name to just Print