Rhinocommon Print PDF example?

I’m trying to print the top viewport to a pdf. I’ve tried scripting “-print” but when I do the “go” bit nothing happens. I saw someone was having this same issue back with Rhino 6. Or perhaps we’re missing something?

I’m trying the other route too with Rhino.FileIO.FilePdf.Create, but I’m not making much progress. Is there perhaps an example available showing how to do this?

Thanks in advance for your help,
Sam

Hi Sam,

I created a simple class to create a pdf document and add pages to it.
Does this example get you started:
NOTE: it seems something is wrong with the size calculations

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

class PDF_writer(object):
    
    DPI = 300
    
    def __init__(self, pdffilepath, width = 210, height = 297, DPI = 300):
        self.pdf = Rhino.FileIO.FilePdf.Create()
        self.pdffilepath = pdffilepath
        
        self.width = width
        self.height = height
        self.DPI = DPI
        
        size_x = int((width/25.4)*DPI)
        size_y = int((height/25.4)*DPI)
        self.size = System.Drawing.Size(size_x,size_y)
        
        self.colormode = Rhino.Display.ViewCaptureSettings.ColorMode.DisplayColor

    def addpage(self, view):
        settings = Rhino.Display.ViewCaptureSettings(view, self.size, self.DPI)
        settings.OutputColor == self.colormode
        self.pdf.AddPage(settings)

    def write(self):
        self.pdf.Write(self.pdffilepath)

# get filename fr PDF
pdf_file = rs.SaveFileName('pdf name', filter = 'pdf files|*.pdf|')

width = 210 # assumed document units mm
height = 297 # assumed document units mm
DPI = 300

# create instance of PDF_writer()
pdf = PDF_writer(pdf_file, width, height, DPI)

# Find first view named 'Top'
top_view  = sc.doc.Views.Find('Top',True)

# add found top view as page
pdf.addpage(top_view)

pdf.write()

HTH
-Willem

2 Likes

Got it, thanks!

Sam

But I’m having a bit of a hard time with getting things to scale and the paper to the proper dimensions. The scale I get reasonably okay in a very roundabout way. But the paper size is always too big. I set the view pixels and DPI such that they represent a legal sized piece of paper (8.5 X 14 inches). But when I create the PDF, it has dimensions 9.3 x 15.4 inches. It’s like there are some margins being added on top. But in the ViewCaptureSettings I set set the margins to 0.

I’m essentially just trying to print the top view to PDF legal sized paper with scale 1. Any way you could tweak your example to achieve this?

Thank you,
Sam

Figured that out too, am set now, thanks for the help.

Is there a way to call this from outside Rhino? E.g. from an external python program?

Hi @Gerrard_Hickson

You will need to run this inside Rhino to create and add pages from Rhino views

If you are looking to merge existing pdf’s, there are existing python packages available.

Does that make sense?

  • Willem

In the case of going outside (or even from inside) Rhino, I’d look into pdfSharp or even QuestPdf…