PDF batch printing - fully scriptable print command required

Last night I printed 90 separate single page PDF files with different file names in 12 minutes. Most of what’s visible in the PDF is generated parametrically in Grasshopper. The text on the layouts is updated from one PDF to the next with Document User Text. I used a script to copy the filename of each PDF to the clipboard. To automate the whole print process and reduce the number of clicks required, I animated a slider. I don’t care about the image output of the slider, but it makes my slider move by itself so that’s all I needed. Once the process starts, it updates the first layout. A Python script runs the Rhino command _-Print Preview and unfortunately it can’t be fully scripted, or I just don’t know how. So, the print preview shows up and I have to hit Enter once to get out of the preview dialog. In the save as window, I paste the filename manually with a button on a SpaceMousePro that executes a CTRL+V. Then one more Enter, the PDF is saved and the process starts again with Grasshopper updating the layout.

I’d like to say that generating layouts in Grasshopper has its perks but printing is not it yet.

The main advantage is the parametric attributes and small number of layouts, my file has just 7 layouts for a few hundred parts and each part takes one page.

Finally once more like in other topics, I’d really appreciate if the whole printing could be scripted.

2 Likes

I have never tried but are you not able to script _-Print rather than _-Print Preview ? Is this one of those things which are possible in Rhino but not in Grasshopper ?

_Print by itself opens the print setup dialog. It’s the same as running _-Print Preview.

_-Print results in a command line option:

image

OK that is what I was expecting, in which case you can script all of the command line options oder …?

It seems to me the Enter to get out of the Print Setup dialog and pasting the filename currently can’t be scripted.

Ahh yes sorry I see now, that is a shame… it looks like this is tracked here with no recent activity

https://mcneel.myjetbrains.com/youtrack/issue/RH-61278

This exists though:
https://developer.rhino3d.com/wip/api/RhinoCommon/html/Methods_T_Rhino_FileIO_FilePdf.htm

1 Like

Interesting :slight_smile:

Found this here:

Yes, I’m either using is wrong or it’s kinda bugged for large amounts of pages.

As macro with “Microsoft Print To PDF” work’s here
_-Print S D P “Microsoft Print to PDF” Enter Enter G "C:\Users\xxxxYourUserNamexxxx\Desktop\$myfile"
$myfile=FileName without file extension PDF

2 Likes

Bit of copy paste from RhinoHelp to PythonEditor.

import rhinoscriptsyntax as rs
myfile = "testfile"
rs.Command('_-Print S D P "Microsoft Print to PDF" Enter Enter G C:\Users\XXXX\Desktop\{}'.format(myfile))
result = rs.LastCommandResult()
if result==0:
    print "The command completed."
else:
    print "The command did not complete."
2 Likes

Thanks @Asterisk for mentioning FilePDF. So far it works fine with single page PDF’s and I found out how I can add more pages.

import Rhino
import scriptcontext as sc
import System.Drawing
import rhinoscriptsyntax as rs

# create a new PDF document
pdf = Rhino.FileIO.FilePdf.Create()
dpi = 600
size = System.Drawing.Size(11*dpi, 8.5*dpi)

page_views = sc.doc.Views.GetPageViews()

for page_view in page_views:
    settings = Rhino.Display.ViewCaptureSettings(page_view, size, dpi)
    pdf.AddPage(settings)

#write the pdf file to disk
#filename = rs.SaveFileName()
pdf.Write(filename)
1 Like

Thanks for your contribution too @eddi

Now we have two options :slight_smile:

The scripted print command to create rastered PDF files and the FilePDF solution to create vectorized PDF’s.

For raster output:
Macro:
-Print S D P “Microsoft Print to PDF” O=Raster Enter Enter G "C:\Users\xUserNamex\Desktop\$myfile"

Py

import rhinoscriptsyntax as rs

xuser = "here" # Set Your username
xoutput = "Raster" # Set Vector for vector output
myfile = "testfile" # Set file name, without extension

rs.Command('_-Print S D P "Microsoft Print to PDF" O={} Enter Enter G C:\Users\{}\Desktop\{}'.format(xoutput,xuser,myfile))
result = rs.LastCommandResult()
if result==0:
    print "The command completed."
else:
    print "The command did not complete."