Export 2D with Scenes to PDF


I tried this Example Code in the Ruby Console to export into the PDF-Format, but “Status” returns “false” and it does not create the PDF-File.
The API documentation notes, that this Code is for SketchUP 6.0 - I’m using 2015. Maybe something has changes since 6.0 ?

Here is the Log of the Console:

model = Sketchup.active_model
show_summary = true
status = model.export 'c:/my_export.pdf', show_summary

Does anybody know what wrong ?

My goal is, to create a script, which exports every Scene to (2D-)PDF.

Kind regards

Well you are attempting to use the wrong method from the wrong object.

EDIT: SketchUp Pro 2016+ includes PDF export capability for the Model#export method.

(Hidden response about using View#write_image.)

A Scene is a view-based feature.

However I don’t think the API write_image method for PDF format works on PC.
(It might on the Mac editions.) And PDF extensibility is SketchUp Pro only.

So, you’d best use a 3rd party library.

(Moved to Developers > Ruby API category.)

I guess “Sketchup::View#write_image” is for pixelformat only ?
I need a vector based format (pdf or eps) not a pixelformat.
I’m working on a Sketchup Pro Version and there I can “File->Export…->2D-Graphic…->PDF-Format” - but how can I run this by Script ?

As I said (above,) it is not exposed to the API.

And it matters not, as 2D Graphic export is Raster, and you make clear …

(Updated topic title and tags to be more in line with OP’s desires.)

Sorry, I’m not sure if I understand you right:
You wanna say that “File->Export…->2D-Graphic…->PDF-Format (Vectorformat not Raster)” is not possible by Script, right ?


I said that:


File > Export… > 2D Graphic… > PDF is RASTER format

… as well as all other formats presented in the 2D Graphic export dialog.

EDIT: Wrong it is VECTOR for PDF (even on the PC.)

(2) Back to your original question:

model = Sketchup.active_model
show_summary = true
#=> true

status = model.export 'c:/my_export.pdf', show_summary
#=> false

The error above is that the pdf option must use an options hash.

The example shown (currently in the docs) is INCORRECT.

Should be:

# Export pdf file on a PC, showing a summary when complete.
options_hash = {
  :show_summary => true,
  :output_profile_lines => false,
  :map_fonts => false,
  :model_units => Length::Meter
status = model.export( 'c:/my_export.pdf', options_hash )

(I’ll log a API doc issue.)

And because Ruby collects hash arguments into a hash automatically, you can call it like:

status = model.export( 'c:/my_export.pdf', :show_summary => true )
# or for Ruby 2.0+ usng named arguments:
status = model.export( 'c:/my_export.pdf', show_summary: true )

… if say you only wanted to change 1 or 2 options from the defaults.

For all defaults, try:

status = model.export( 'c:/my_export.pdf', {} )

(A) BUT, it looks like it uses the current view, and the PDF page looks like RASTER output.

(B) It only outputs one page (the current scene page’s view.)

So, as said above, you should be looking for a 3rd party PDF library.

Okay, no, I opened the output file in LibreOffice Draw, and it is indeed vector.

To (1)
…yes I just wanted to write that :wink:
So how can I do this by Script ?

To (2)
It still returns “false”.

Dude! Are you still on SketchUp Pro 2015 ?

The API doc says clearly:

SketchUp Pro 2016+ includes PDF export capability.

As long as you don’t care that each scene is output to a separate pdf file:

You have to first have SketchUp Pro 2016 or higher.

You call the UI.select_directory method to get an output path,

(And it’s best to switch off scene transitions whilst doing this export loop. Restore the previous setting afterward.)
See: Sketchup::OptionsManager

Then you iterate the model’s Pages collection, repeatedly calling the pdf export, using the model name + page name as the export file name.

But there are some gotchas:

You need to wait until SketchUp is done writing each file before switching to the next scene page.
See the Ruby standard File class, ie #exist? method.

And you need to wait until scene transition is done, before calling each export.
See: FrameChangeObserver

Have fun learning Ruby.

The API only says “SketchUp Pro 2016+ includes PDF export capability”.
BUT SketchUp Pro 2015 does too, else I woudn’t be able to “File->Export…->2D-Graphic…->PDF-Format (Vectorformat not Raster)”.
And “SketchUp Pro 2016+ includes PDF export capability” says nothing about the API itself, it only says about the capability of SketchUP Pro 2016 in general.

So once again, an please be so kind to give me answer and not new question :wink:
How to do “File->Export…->2D-Graphic…->PDF-Format (Vectorformat not Raster)” by Script in SketchUp Pro 2015 ?
Is it possible ? Yes or no ?

OK, I can try that, but I still don’t understand why it shoudn’t be possible with SketchUp Pro 2015.

“Have fun learning Ruby” ?
This kind off missinformation should be fun ?

it’s vector pdf export on a mac from the menu or from ruby…

if you use a standard view and Parallel Projection you can also set scale…


Just tried with SketchUP Pro 2016 Trial.

There opens a Window that sais “PDF Error writing export file” when I write “status = model.export( ‘c:/my_export.pdf’, options_hash )”.

When I use “status = model.export( “c:\my_export.pdf”, options_hash )” instead of “status = model.export( ‘c:/my_export.pdf’, options_hash )” then it returns “true” BUT the file “c:\my_export.pdf” does not exist anywhere.

Listen, you are reading from the API documentation,
not a general document about SketchUp in general.
So your statement does not apply.

You cannot do that. Double-quoted strings in Ruby use interpolation and escape characters.
"" is the escape character, so:

path = "c:\my_export.pdf"
#=> c:my_export.pdf

There are special escape sequences, \n is a newline, \r is a carriage return, \t is a tab.
You must escape a slash in double-quoted strings:

path = "c:\\my_export.pdf"
#=> c:\my_export.pdf

Using the SketchUp API requires first learning Ruby.
I created a nice pinned topic of learning resources here in this category:

[quote=“john_drivenupthewall, post:14, topic:37909”][quote=“DanRathbun, post:7, topic:37909”]
File > Export… > 2D Graphic… > PDF is RASTER format

it’s vector pdf export on a mac from the menu or from ruby…

It is also on the PC. I got confused about what was on the 2D
import dialog and what was on the 3D dialog.

The confusion came from that there is a different API means to export raster from vector.

(I got too many things going on at the same time.)

NO. Not in SketchUp 2015 Pro.

Because, this export feature was added into the API when SketchUp 2016 was released.

I tested it this morning in SU2016 Pro, and it works fine.

(I’m done with this thread. Setting it on “Muted”.)

It works now, I used another drive to save the PDF.
Probably c: on my workstation has some kind of security restrictions.