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("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:
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.
var pageWidth = 210;
var pageHeight = 297;
var bb = document.getBoundingBox();
var scale = 10.0;
document.setVariable(
"PageSettings/PaperUnit",
RS.
Millimeter);
document.setVariable("PageSettings/PaperWidth", pageWidth);
document.setVariable("PageSettings/PaperHeight", pageHeight);
document.setVariable("PageSettings/PageOrientation", "Portrait");
document.setVariable("ColorSettings/ColorMode", "FullColor");
document.setVariable(
"ColorSettings/BackgroundColor",
new RColor(
"white"));
document.setVariable("PageSettings/Scale", "10:1");
document.setVariable("PageSettings/OffsetX", -(pageWidth/scale - bb.getWidth()) / 2);
document.setVariable("PageSettings/OffsetY", -(pageHeight/scale - bb.getHeight()) / 2);
document.setVariable("MultiPageSettings/Rows", 1);
document.setVariable("MultiPageSettings/Columns", 1);
document.setVariable("MultiPageSettings/PrintCropMarks", false);
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("scripts/File/Print/Print.js");
qApp.applicationName = "MyApplication";
var document =
new RDocument(storage, spatialIndex);
operation.apply(document);
view.setScene(scene);
var pageWidth = 210;
var pageHeight = 297;
var bb = document.getBoundingBox();
var scale = 10.0;
document.setVariable(
"PageSettings/PaperUnit",
RS.
Millimeter);
document.setVariable("PageSettings/PaperWidth", pageWidth);
document.setVariable("PageSettings/PaperHeight", pageHeight);
document.setVariable("PageSettings/PageOrientation", "Portrait");
document.setVariable("ColorSettings/ColorMode", "FullColor");
document.setVariable(
"ColorSettings/BackgroundColor",
new RColor(
"white"));
document.setVariable("PageSettings/Scale", "10:1");
document.setVariable("PageSettings/OffsetX", -(pageWidth/scale - bb.getWidth()) / 2);
document.setVariable("PageSettings/OffsetY", -(pageHeight/scale - bb.getHeight()) / 2);
document.setVariable("MultiPageSettings/Rows", 1);
document.setVariable("MultiPageSettings/Columns", 1);
document.setVariable("MultiPageSettings/PrintCropMarks", false);
document.setVariable("PageTagSettings/EnablePageTags", false);
var print =
new Print(undefined, document, view);
print.print("example.pdf");