export to pdf

Discussion forum for C++ and script developers who are using the QCAD development platform or who are looking to contribute to QCAD (translations, documentation, etc).

Moderator: andrew

Forum rules

Always indicate your operating system and QCAD version.

Attach drawing files, scripts and screenshots.

Post one question per topic.

Post Reply
neri alamanni
Newbie Member
Posts: 8
Joined: Thu Nov 28, 2024 5:47 pm

export to pdf

Post by neri alamanni » Thu Nov 28, 2024 5:52 pm

Hi
i developed this small script, whose target is loading a block from disk into a new document, save the document as dwg file and export as a pdf file
the dwg file is created correctly but the pdf file is empty
This is the script
include("C:/Program Files/QCAD/scripts/File/Print/Print.js");

var ms = new RMemoryStorage();
var si = createSpatialIndex();
var docItem = new RDocument(ms, si);
var diItem = new RDocumentInterface(docItem);
diItem.setNotifyListeners(false);
diItem.importFile("c:/temp/2900 SpeQ.dwg", "", false);
var op = new RPasteOperation(docItem);
op.setText("Insert FPT Block");
op.setOffset(new RVector(1,400)); // the position where you want to place the item
op.setBlockName("MyBlock"); // if you want to insert the item as block, set a block name here
op.setScale(1.0); // set a scale if desired
op.setRotation(0.0);
var di = this.getDocumentInterface(); // this is the interface to the drawing you are working on
di.applyOperation(op); // apply the paste operation
autoZoom();
var name="c:/temp/export_test.dwg";
var version="";
var fileName = di.getCorrectedFileName(name, version);
di.exportFile(name, version);
//! [view]
var scene = new RGraphicsSceneQt(di);
var view = new RGraphicsViewImage();
view.setScene(scene);
//! [view]

//! [settings]
// set up page and paper settings as document variables:
var pageWidth = 2100;
var pageHeight = 2970;
var bb = docItem.getBoundingBox(true, true);
var scale = 1.0;

// paper unit:
docItem.setVariable("PageSettings/PaperUnit", RS.Millimeter);
// paper width:
docItem.setVariable("PageSettings/PaperWidth", pageWidth);
// paper height:
docItem.setVariable("PageSettings/PaperHeight", pageHeight);

// page orientation ("Portrait" | "Landscape"):
docItem.setVariable("PageSettings/PageOrientation", "Portrait");

// color mode ("FullColor" | "GrayScale" | "BlackWhite"):
docItem.setVariable("ColorSettings/ColorMode", "FullColor");

// background color:
docItem.setVariable("ColorSettings/BackgroundColor", new RColor("white"));

// drawing scale as string:
docItem.setVariable("PageSettings/Scale", "10:1");

// offset of drawing origin to lower left corner in drawing units:
docItem.setVariable("PageSettings/OffsetX", -(pageWidth/scale - bb.getWidth()) / 2);
docItem.setVariable("PageSettings/OffsetY", -(pageHeight/scale - bb.getHeight()) / 2);

// number of pages (rows / columns):
docItem.setVariable("MultiPageSettings/Rows", 1);
docItem.setVariable("MultiPageSettings/Columns", 1);

// switch on / off crop marks:
docItem.setVariable("MultiPageSettings/PrintCropMarks", false);

// switch on / off page tags:
docItem.setVariable("PageTagSettings/EnablePageTags", false);
//! [settings]

//! [export]
var print = new Print(undefined, docItem, view);
print.print("c:/temp/example.pdf");
//! [export]
Quit();

Can you pls suggest me where is the problem?
thank you
regards
neri

User avatar
andrew
Site Admin
Posts: 8774
Joined: Fri Mar 30, 2007 6:07 am

Re: export to pdf

Post by andrew » Fri Nov 29, 2024 10:33 am

Can you try:

Code: Select all

var doc = this.getDocument();
var print = new Print(undefined, doc, view);
print.autoFit(di, false);
print.print("c:/temp/example.pdf");
Note that you are setting the parameters for the document of the imported item, not the composed document. It seems that you are also printing the item, not the composed document - not sure if that is intended. Be sure to keep doc/di and docItem/diItem separate.

Just a hunch, it could also be that your scale / offset are not right.

CVH
Premier Member
Posts: 4919
Joined: Wed Sep 27, 2017 4:17 pm

Re: export to pdf

Post by CVH » Fri Nov 29, 2024 10:55 am

Hi, and welcome to the QCAD forum.

First, please read the forum rules.
Example files would be welcome but I managed without. :wink:

Second, please place script code in a code panel.
Edit your post, select all the code and tick the [<>] button above the text panel.

Nevertheless I tried to run your code in the Script Shell:
There were two errors:
- Error: include: cannot read file 'C:/Program Files/QCAD/scripts/File/Print/Print.js'
- ReferenceError: Can't find variable: Quit

In a standard installation most QCAD resources are included in compiled form.
It may even be that there is no physical sub-folder '/File' or '/File/Print' in your installation path.
include("scripts/File/Print/Print.js"); is a functional syntax.

Quit() on itself is not a method in ECMAScript, simply remove it.


Because an export to DXF/DWG is made very simple the code creates a new file called 'export_test.dwg'.
The content is mostly identical as 'the drawing you are working on'
When I ran the script that was the active open drawing with irrelevant content.

Before the test I created the path 'c:/temp/' and included a file '2900 SpeQ.dwg' with some content.
A new Block Definition called 'MyBlock' was added with this content and a new Block Reference at (1, 400).

autoZoom(); is part of the simple API.
simple.js is not explicitly included but typically available when using the GUI.
It does nothing more than the GUI command Auto Zoom (ZA).
It only affects the rendering of the current drawing on screen.

Although print.js could not be located it creates an empty PDF file.
Again, the print method would be available when using the GUI.

Your print section is basically a copy of the command line example ExPdfExport.js
But there are some subtle differences like a page that is 100 times an A4.

It seems you are attempting to export docItem what is the off-screen document with the imported content from '2900 SpeQ.dwg'.
And not the content of 'export_test.dwg' what is now also the current active drawing.
Remark that there is not yet an RDocument reference defined for this.
The bounding box is also taken from the off-screen document and I have no idea how large that is in your case.
Your code omits setting the drawing unit for the off-screen document.
I also adapted the creation of the spatial index conform ExPdfExport.js

Nevertheless we expect something visible in the exported PDF.
And then I tried it with forcing a Print Preview on the 2900 SpeQ.dwg document.
Content in my case is 190 x 190, left lower corner is (10, 10).
=> OffsetX = 955, OffsetY = 1390

And that is way off from where the content is.
=> The PDF shows nothing but empty space.

Bottom line:
The example code in ExPdfExport.js does not account for the lower left position of the bounding box.
All fine for the example triangle with one corner at the origin. :wink:
The used scale factor 10:1 explains itself knowing that the triangle is only 10 x 8.66 large.

In the mean time Andrew came to the same conclusions ...

Regards,
CVH

neri alamanni
Newbie Member
Posts: 8
Joined: Thu Nov 28, 2024 5:47 pm

Re: export to pdf

Post by neri alamanni » Wed Dec 04, 2024 6:02 pm

Hi
thank you for your replies. Now i can see the correct output in the pdf file
neri

Post Reply

Return to “QCAD Programming, Script Programming and Contributing”