QCAD
Open Source 2D CAD
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:

1 // include class Print:
2 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:

1 var scene = new RGraphicsSceneQt(di);
2 var view = new RGraphicsViewImage();
3 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.

1 // set up page and paper settings as document variables:
2 var pageWidth = 210;
3 var pageHeight = 297;
4 var bb = document.getBoundingBox(true, true);
5 var scale = 10.0;
6 
7 // paper unit:
8 document.setVariable("PageSettings/PaperUnit", RS.Millimeter);
9 // paper width:
10 document.setVariable("PageSettings/PaperWidth", pageWidth);
11 // paper height:
12 document.setVariable("PageSettings/PaperHeight", pageHeight);
13 
14 // page orientation ("Portrait" | "Landscape"):
15 document.setVariable("PageSettings/PageOrientation", "Portrait");
16 
17 // color mode ("FullColor" | "GrayScale" | "BlackWhite"):
18 document.setVariable("ColorSettings/ColorMode", "FullColor");
19 
20 // background color:
21 document.setVariable("ColorSettings/BackgroundColor", new RColor("white"));
22 
23 // drawing scale as string:
24 document.setVariable("PageSettings/Scale", "10:1");
25 
26 // offset of drawing origin to lower left corner in drawing units:
27 document.setVariable("PageSettings/OffsetX", -(pageWidth/scale - bb.getWidth()) / 2);
28 document.setVariable("PageSettings/OffsetY", -(pageHeight/scale - bb.getHeight()) / 2);
29 
30 // number of pages (rows / columns):
31 document.setVariable("MultiPageSettings/Rows", 1);
32 document.setVariable("MultiPageSettings/Columns", 1);
33 
34 // switch on / off crop marks:
35 document.setVariable("MultiPageSettings/PrintCropMarks", false);
36 
37 // switch on / off page tags:
38 document.setVariable("PageTagSettings/EnablePageTags", false);

The actual export is done through funciton 'print':

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

Complete code:

1 //! [include]
2 // include class Print:
3 include("scripts/File/Print/Print.js");
4 //! [include]
5 
6 //! [init]
7 // init application name:
8 qApp.applicationName = "MyApplication";
9 
10 //! [document]
11 var storage = new RMemoryStorage();
12 var spatialIndex = new RSpatialIndexNavel();
13 var document = new RDocument(storage, spatialIndex);
14 document.setUnit(RS.Millimeter);
15 
16 // create document interface (needed for file export functionality):
17 var di = new RDocumentInterface(document);
18 //! [document]
19 
20 //! [operation]
21 // create an operation:
22 var operation = new RAddObjectsOperation();
23 
24 // corner point of the triangle::
25 var p1 = new RVector(0, 0);
26 var p2 = new RVector(10, 0);
27 var p3 = new RVector(5, Math.sin(RMath.deg2rad(60))*10);
28 
29 // add line entity to operation:
30 operation.addObject(new RLineEntity(document, new RLineData(p1, p2)));
31 operation.addObject(new RLineEntity(document, new RLineData(p2, p3)));
32 operation.addObject(new RLineEntity(document, new RLineData(p3, p1)));
33 
34 // apply operation to document:
35 operation.apply(document);
36 //! [operation]
37 
38 //! [view]
39 var scene = new RGraphicsSceneQt(di);
40 var view = new RGraphicsViewImage();
41 view.setScene(scene);
42 //! [view]
43 
44 //! [settings]
45 // set up page and paper settings as document variables:
46 var pageWidth = 210;
47 var pageHeight = 297;
48 var bb = document.getBoundingBox(true, true);
49 var scale = 10.0;
50 
51 // paper unit:
52 document.setVariable("PageSettings/PaperUnit", RS.Millimeter);
53 // paper width:
54 document.setVariable("PageSettings/PaperWidth", pageWidth);
55 // paper height:
56 document.setVariable("PageSettings/PaperHeight", pageHeight);
57 
58 // page orientation ("Portrait" | "Landscape"):
59 document.setVariable("PageSettings/PageOrientation", "Portrait");
60 
61 // color mode ("FullColor" | "GrayScale" | "BlackWhite"):
62 document.setVariable("ColorSettings/ColorMode", "FullColor");
63 
64 // background color:
65 document.setVariable("ColorSettings/BackgroundColor", new RColor("white"));
66 
67 // drawing scale as string:
68 document.setVariable("PageSettings/Scale", "10:1");
69 
70 // offset of drawing origin to lower left corner in drawing units:
71 document.setVariable("PageSettings/OffsetX", -(pageWidth/scale - bb.getWidth()) / 2);
72 document.setVariable("PageSettings/OffsetY", -(pageHeight/scale - bb.getHeight()) / 2);
73 
74 // number of pages (rows / columns):
75 document.setVariable("MultiPageSettings/Rows", 1);
76 document.setVariable("MultiPageSettings/Columns", 1);
77 
78 // switch on / off crop marks:
79 document.setVariable("MultiPageSettings/PrintCropMarks", false);
80 
81 // switch on / off page tags:
82 document.setVariable("PageTagSettings/EnablePageTags", false);
83 //! [settings]
84 
85 //! [export]
86 var print = new Print(undefined, document, view);
87 print.print("example.pdf");
88 //! [export]
89