printing PDFs from Revit…why is it so hard?

image by guys from office space

Disclaimer: 

If you don’t have to, please don’t reinvent the wheel. I am a proponent of just going out and buying the tools you need to do your job, rather than re-making them yourself. It’s a bit inefficient to solve the same problem that others have already solved. With that said, the best tool on the market for running PDF prints is RTV Tools. Get that if you can spend a few bucks. The rest of us poor souls, read on! 

I put Revit’s name in the title, but to be honest the responsibility for this lies somewhere in-between Autodesk and Adobe. First, let’s make it clear, that for a piece of software that main’s job is to create documentation drawings of buildings (99% of the time distributed in PDF format), not to have its own PDF creator, or at least licensed and implemented one of the handful available ones…I just have no words for this Autodesk. This condition, that we have been dealing with since Revit’s inception some 15+ years ago, is just not acceptable. Sure, it’s hunky-dory to let users specify their own Printer, but for god’s sake, Autodesk, please also give them an embedded one that comes with the software. That would also allow you to cater to needs of those of us, that want a little more out of the software than a typical end user. I am speaking of the development community that wants to build automation tools on top of your software, and having access to embedded PDF creator, with ability to specify things like JPG Compression, Security protocols or simply FILE NAME. Yes! File name, and location that will not be overridden by the 3rd party PDF creator like Adobe’s Distiller. Now that we are on the subject of Adobe and its free PDF Reader, I just have no words for how shitty that thing is. Yes, it’s free, but it’s also severely crippled, and ON PURPOSE! What the hell were you thinking Adobe, always launching Acro Tray process in the background, and leaving it open even, when printing is done. WTF? Yes, the paid version of your software has more support, but still…that’s just sad. Anyways, let’s talk about some solutions here:

You probably know that if you set Printing Preferences for Adobe PDF printer, like this…

…you should be able to send a printing job of multiple sheets, and you would not be prompted to specify location for each one individually. That’s great if you are content with the pre-defined print location. What happens when you are building a plug-in and you want your users to be able to specify custom file names and file locations? In that case there is so far been one option:

Override the PrinterJobControl registry input! What?! Yes, in order to specify a file name, one must dabble with Registry. Thanks Autodesk! That’s swell! Here’s how and why:

Adobe PDF, normally prompts user for a new file location. However, there is a way to override that, if one overrides the PrinterJobControl subkey, with a Key/Value pair specifically telling Adobe that for the next printing job submitted from Revit.exe process, you want it to print to a specific filename and location. Here’s a small utility that will do that for you:

As you can see this will only work for Adobe PDF printer. I also have a global variable named CURRENT_REVIT_VERSION stored in my Settings file. This should be either 2016 or 2017 depending on what version of Revit I am compiling my plug-in for. Basically, you would need to call this method before every single sheet is submitted to Revit’s Print Manager for printing. That way, you always override the next print job’s name, and location and Adobe won’t prompt for filename.

Now, it might ask to replace an existing file if you didn’t uncheck that setting in the Printing Preferences dialog box. To avoid seeing that prompt, just use the regular File.Exists() method and File.Delete() to clean up before printing. Something like this will do:

Now, I also often do a post processing routine. Take for example, Zipping up all of your files to be sent to consultant or even combining them into a single PDF, if you are that adventurous. Since printing a PDF can take quite some time, even minutes, which in computer world is freaking decades, you will need a small utility to wait for the last file to print before starting your post processing. Here’s one I copy-pasted from Stack Overflow (copying and pasting from SO is an art.):

I call it like this:

The “lastCreatedFileName” variable is as you can guess the name of the last sheet that I have submitted to Print Manager. I am only really checking the last file submitted, since all others would be done, before I can submit my last one. My wait routine, has 1000 loops each halting the execution for 300 ms for a total of 3min. If printing of your last sheet takes more than 3min, it will most likely be skipped in the post-processing routine. Just an FYI.

I hope this helps. Cheers!

13 Comments

  1. Viktor Kuzev says:

    Great post and the “Office Space” reference is hilarious. I can’t count how many weird looks I’ve taken from colleagues when stating that Revit does not have its own PDF printer.
    I still feel quite uncomfortable even looking at c# code though :)

  2. JefferyHarwood says:

    This is great. Thank you for the lesson.

  3. Great blog my friend :-)

  4. Konrad, have you looked at other free pdf printers? A lot of people use cutePDF.
    In the past line used a schedule and DOS batch file to rename after the fact. Clunky but robust.
    A python version for using in Dynamo world be great.

  5. 4bimferdie says:

    Hi Konrad,

    Nice blog…i hear you. 10 years ago i have same problem with AutocCAD, it has no built-pdf creator. I have to make my own, now i face the same thing for years now i am only happy with plotting via the revit ui using cutepdf. But I recently discovered the power of dynamo so i joined the dynamo forum. At first i was searching for a way to create pdf with minimal user input like what i have done in autocad thru lisp and scripts. But i have no luck, what you have done in the pdf creation side in dynamo are amazing well done to you.

    I think i can recreate my pdf printing program again using dynamo. I have very positive results. I will post it in the dynamo forum once i am happy everything is working. I will then use it in revit to created pdfs with customized filenames etc.

    Thanks

  6. Michael Park says:

    Hello,

    I made a few tweaks to the code in one of your old articles that allows you to send in the list of filenames you want to export (so you can have a separate filename for each page you are exporting.) This allows me to have all the files named in the first place once you choose where you want the files to save. I have attached a zip file of the .dyn file and the .py file for my changes. Let me know if this is helpful for you. Just a bit easier than needing to use the rename files nodes as it names it correctly in the first place. (In my opinion).

    Cheers,

    Michael

Leave a Comment