Export / Print to pdf: Is there a way to get a vector print?

Post here for help on using FreeCAD's graphical user interface (GUI).
Forum rules
and Helpful information
IMPORTANT: Please click here and read this first, before asking for help

Also, be nice to others! Read the FreeCAD code of conduct!
User avatar
Roland
Posts: 333
Joined: Fri Aug 21, 2015 2:20 pm

Export / Print to pdf: Is there a way to get a vector print?

Post by Roland »

Whether I export or print my Drawing to pdf ... I get a raster image.

Disadvantages:
Text no longer editable (since font vectors projected as rasters) (although I could do an OCR, but that's a detour)
Extremely large files (What a waste of memory)
Low printing quality, so long as files should remain sufficiently small for handling

Is it not a pity that conversion to pdf implies such a loss of quality?

Is there a way to get a vector print?

Thx

Roland
User avatar
Roland
Posts: 333
Joined: Fri Aug 21, 2015 2:20 pm

Re: Export / Print to pdf: Is there a way to get a vector print?

Post by Roland »

Found my own solution.

For those who are interested:
Combo View: Select Page for exporting
File, Export, Type: Drawing (*.svg, etc)

Open exported file in Inkscape. From there do not print to pdfprinter, but rather Save as ... *.pdf.

Bingo.

Result:
10 times smaller files than with FreeCad File, Export PDF.
All the other advantages of a vector print

Is there a shortcut for this procedure inside FreeCAD ?

Greetz,

Roland
triplus
Veteran
Posts: 9475
Joined: Mon Dec 12, 2011 4:45 pm

Re: Export / Print to pdf: Is there a way to get a vector print?

Post by triplus »

Good question. I think it comes down to Qt support for printing out PDF files by using vector back-end. Quick Google search indicates this functionality isn't provided in Qt by default therefore probably custom solution would need to be coded (probably a lot of work would be needed). Anyway will leave that up to developers.

As for:
Is there a shortcut for this procedure inside FreeCAD ?
Probably custom macro could be the solution (to automate the printing to PDF by using Inkscape (command line) from FreeCAD).
triplus
Veteran
Posts: 9475
Joined: Mon Dec 12, 2011 4:45 pm

Re: Export / Print to pdf: Is there a way to get a vector print?

Post by triplus »

I created a drawing and exported it to SVG file and after on Ubuntu:

Code: Select all

inkscape -f FILENAME.svg -A FILENAME.pdf
This procedure works quite good based on first observations. Probably there is a way to make this more automatic. By getting SVG image from drawing page /tmp location and making subprocess call to Inkscape.

Basically in the end it could be done as it is done for POV-Ray support offered in FreeCAD. If user would set Inkscape path in Preferences FreeCAD would use Inkscape to create PDF file instead of default option that puts rasterized image in PDF file.
triplus
Veteran
Posts: 9475
Joined: Mon Dec 12, 2011 4:45 pm

Re: Export / Print to pdf: Is there a way to get a vector print?

Post by triplus »

OK i created Export PDF macro. It uses Inkscape to do the SVG to PDF conversion. Customize ToolsBar instructions can be used to add tool button in Drawing WB and therefore to be able to use Export PDF macro with ease when needed.

Code: Select all

import os
import subprocess
from PySide import QtGui

obj = FreeCADGui.Selection.getSelection()[0]

# Path to Inkscape executable
inkscape_path = "/usr/bin/inkscape"
# Exported PDF file location
file_location = os.path.expanduser("~" + os.sep + obj.Label + ".pdf")

# ------------------------------------------------------------------------------------
try:
    page = getattr(obj, 'PageResult')
except AttributeError:
    QtGui.QMessageBox.warning(None,"Export PDF", "Page object has not been selected.")
else:
    file_exists = os.path.isfile(file_location)
    if file_exists == True:
        QtGui.QMessageBox.warning(None,"Export PDF", "A file with the name " + obj.Label + ".pdf already exists:\n\n" + file_location)
    else:
        call_inkscape = [inkscape_path, "-f", obj.PageResult, "-A", file_location]
        subprocess.call(call_inkscape)
        QtGui.QMessageBox.information(None,"Export PDF", "Successfully exported:\n\n" + file_location)

What does it do:

It exports drawing page to PDF file using Inkscape. Instead of bitmap image inserted in PDF file elements like text are searchable and exported PDF file size can be smaller when the drawings do not have big number of elements in it.

How to use it:
  • Select Page object in Tree View.
  • Run Export PDF macro.
User settings:

First thing the users needs to check is if the path to Inkscape executable is set correctly. It should work out of the box on Ubuntu (i didn't test the Export PDF macro on Windows but the path to Inkscape executable needs to be changed).
User avatar
Roland
Posts: 333
Joined: Fri Aug 21, 2015 2:20 pm

Re: Export / Print to pdf: Is there a way to get a vector print?

Post by Roland »

That is great, Triplus.

I work under win7. Tried yr code, but not functional. I suppose the commands are linux type. I shall look into it later, and report it here when succesful.

Best regards,

Roland
triplus
Veteran
Posts: 9475
Joined: Mon Dec 12, 2011 4:45 pm

Re: Export / Print to pdf: Is there a way to get a vector print?

Post by triplus »

Roland wrote:That is great, Triplus.

I work under win7. Tried yr code, but not functional. I suppose the commands are linux type. I shall look into it later, and report it here when succesful.

Best regards,

Roland
Hopefully you will just need to change this line:

Code: Select all

inkscape_path = "/usr/bin/inkscape"
To something like this:

Code: Select all

inkscape_path = "path\to\inkscape.exe"
I don't know where Windows 7 stores installed programs but you will probably find that easily. After you can test in CMD if the path is correct by issuing command:

Code: Select all

path\to\inkscape.exe
I am not sure what will happen but i am guessing some sort of confirmation the path is correct will be indicated. Put that path in the macro as discussed earlier and try to run the macro again.
User avatar
sgrogan
Veteran
Posts: 6503
Joined: Wed Oct 22, 2014 5:02 pm

Re: Export / Print to pdf: Is there a way to get a vector print?

Post by sgrogan »

This works for me on Win7 x64

Code: Select all

import os
import subprocess
from PySide import QtGui

obj = FreeCADGui.Selection.getSelection()[0]

# Path to Inkscape executable
inkscape_path = "C:\Program Files\Inkscape\Inkscape.exe"
# Exported PDF file location
file_location = os.path.expanduser("~" + os.sep + "Documents" + obj.Label + ".pdf")

# ------------------------------------------------------------------------------------
try:
    page = getattr(obj, 'PageResult')
except AttributeError:
    QtGui.QMessageBox.warning(None,"Export PDF", "Page object has not been selected.")
else:
    file_exists = os.path.isfile(file_location)
    if file_exists == True:
        QtGui.QMessageBox.warning(None,"Export PDF", "A file with the name " + obj.Label + ".pdf already exists:\n\n" + file_location)
    else:
        call_inkscape = [inkscape_path, "-f", obj.PageResult, "-A", file_location]
        subprocess.call(call_inkscape)
        QtGui.QMessageBox.information(None,"Export PDF", "Successfully exported:\n\n" + file_location)
This is the default Inkscape install, and writes the file to the user/documents folder
"fight the good fight"
chrisb
Veteran
Posts: 51227
Joined: Tue Mar 17, 2015 9:14 am

Re: Export / Print to pdf: Is there a way to get a vector print?

Post by chrisb »

This seems worth living in the Macro Recipes section of the homepage! Who can enter it there?
A Sketcher Lecture with in-depth information is available in English, auf Deutsch, en français, en español.
User avatar
Roland
Posts: 333
Joined: Fri Aug 21, 2015 2:20 pm

Re: Export / Print to pdf: Is there a way to get a vector print?

Post by Roland »

Great, Srogan,

You cracked it for my win7 (64) too!

Thx

Roland
Post Reply