QCAD
Open Source 2D CAD
 All Classes Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
Exporting a Drawing to PDF

To export a document to PDF, we can use the printing framework of QCAD which is itself implemented in ECMAScript. To do this, we simply include the class Print:

// include class Print:
include("scripts/File/Print/Print.js");

To print or export a drawing to PDF, we need a visual representation of the drawing first. This is achieved through a graphics scene with an attached view:

var scene = new RGraphicsSceneQt(di);
var view = new RGraphicsViewImage();
view.setScene(scene);

In most cases, we need to configure the page size, scale factor and other settings before we can produce a usable PDF output. This is done by setting various custom variables in our document. Not that a document might already have these settings, for example if it was previously printed or exported in QCAD.

// set up page and paper settings as document variables:
var pageWidth = 210;
var pageHeight = 297;
var bb = document.getBoundingBox();
var scale = 10.0;
// paper unit:
document.setVariable("PageSettings/PaperUnit", RS.Millimeter);
// paper width:
document.setVariable("PageSettings/PaperWidth", pageWidth);
// paper height:
document.setVariable("PageSettings/PaperHeight", pageHeight);
// page orientation ("Portrait" | "Landscape"):
document.setVariable("PageSettings/PageOrientation", "Portrait");
// color mode ("FullColor" | "GrayScale" | "BlackWhite"):
document.setVariable("ColorSettings/ColorMode", "FullColor");
// background color:
document.setVariable("ColorSettings/BackgroundColor", new RColor("white"));
// drawing scale as string:
document.setVariable("PageSettings/Scale", "10:1");
// offset of drawing origin to lower left corner in drawing units:
document.setVariable("PageSettings/OffsetX", -(pageWidth/scale - bb.getWidth()) / 2);
document.setVariable("PageSettings/OffsetY", -(pageHeight/scale - bb.getHeight()) / 2);
// number of pages (rows / columns):
document.setVariable("MultiPageSettings/Rows", 1);
document.setVariable("MultiPageSettings/Columns", 1);
// switch on / off crop marks:
document.setVariable("MultiPageSettings/PrintCropMarks", false);
// switch on / off page tags:
document.setVariable("PageTagSettings/EnablePageTags", false);

The actual export is done through funciton 'print':

var print = new Print(undefined, document, view);
print.print("example.pdf");

Complete code:

// include class Print:
include("scripts/File/Print/Print.js");
// init application name:
qApp.applicationName = "MyApplication";
var storage = new RMemoryStorage();
var spatialIndex = new RSpatialIndexNavel();
var document = new RDocument(storage, spatialIndex);
document.setUnit(RS.Millimeter);
// create document interface (needed for file export functionality):
var di = new RDocumentInterface(document);
// create an operation:
var operation = new RAddObjectsOperation();
// corner point of the triangle::
var p1 = new RVector(0, 0);
var p2 = new RVector(10, 0);
var p3 = new RVector(5, Math.sin(RMath.deg2rad(60))*10);
// add line entity to operation:
operation.addObject(new RLineEntity(document, new RLineData(p1, p2)));
operation.addObject(new RLineEntity(document, new RLineData(p2, p3)));
operation.addObject(new RLineEntity(document, new RLineData(p3, p1)));
// apply operation to document:
operation.apply(document);
var scene = new RGraphicsSceneQt(di);
var view = new RGraphicsViewImage();
view.setScene(scene);
// set up page and paper settings as document variables:
var pageWidth = 210;
var pageHeight = 297;
var bb = document.getBoundingBox();
var scale = 10.0;
// paper unit:
document.setVariable("PageSettings/PaperUnit", RS.Millimeter);
// paper width:
document.setVariable("PageSettings/PaperWidth", pageWidth);
// paper height:
document.setVariable("PageSettings/PaperHeight", pageHeight);
// page orientation ("Portrait" | "Landscape"):
document.setVariable("PageSettings/PageOrientation", "Portrait");
// color mode ("FullColor" | "GrayScale" | "BlackWhite"):
document.setVariable("ColorSettings/ColorMode", "FullColor");
// background color:
document.setVariable("ColorSettings/BackgroundColor", new RColor("white"));
// drawing scale as string:
document.setVariable("PageSettings/Scale", "10:1");
// offset of drawing origin to lower left corner in drawing units:
document.setVariable("PageSettings/OffsetX", -(pageWidth/scale - bb.getWidth()) / 2);
document.setVariable("PageSettings/OffsetY", -(pageHeight/scale - bb.getHeight()) / 2);
// number of pages (rows / columns):
document.setVariable("MultiPageSettings/Rows", 1);
document.setVariable("MultiPageSettings/Columns", 1);
// switch on / off crop marks:
document.setVariable("MultiPageSettings/PrintCropMarks", false);
// switch on / off page tags:
document.setVariable("PageTagSettings/EnablePageTags", false);
var print = new Print(undefined, document, view);
print.print("example.pdf");