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.
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 ?
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
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."
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)
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."