100% found this document useful (1 vote)
140 views

Create A PDF From QT Creator - QT Forum

The document discusses issues creating a PDF file from Qt Creator code. The original poster provided code attempting to create a PDF but it was not working. Respondents suggested checking the file path and output directory. Another respondent provided a sample code that successfully creates a PDF, which helped the original poster resolve the issue. The discussion continued with the original poster receiving suggestions to avoid painter objects going out of scope.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
100% found this document useful (1 vote)
140 views

Create A PDF From QT Creator - QT Forum

The document discusses issues creating a PDF file from Qt Creator code. The original poster provided code attempting to create a PDF but it was not working. Respondents suggested checking the file path and output directory. Another respondent provided a sample code that successfully creates a PDF, which helped the original poster resolve the issue. The discussion continued with the original poster receiving suggestions to avoid painter objects going out of scope.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 10

7/9/2021 Create a PDF from Qt Creator | Qt Forum

General and Desktop


(/category/10/general-and-desktop)

Create a PDF from Qt Creator
 (/topic/96672.rss)

Important: Please read the Qt Code of Conduct -


https://forum.qt.io/topic/113070/qt-code-of-conduct
(https://forum.qt.io/topic/113070/qt-code-of-conduct)

(/user/thecipo76)
 TheCipo76 (/user/thecipo76)
16 nov 2018 8:31 (/post/493499)

Hi,

i'm trying to create a PDF from Qt Creator

i've tried with this code (Declarated QPainter and QPrinter):

printer.setOutputFormat(QPrinter::PdfFormat);

printer.setOutputFileName("AAA.pdf");

QPainter painter;

if (! painter.begin(&printer)) { // failed to open file

qWarning("failed to open file, is it writable?");

return 1;

}
else {
msg.setText("Printer Begin");

msg.exec();

}
painter.drawText(10, 10, "Primo PDF Creato!");
painter.drawText(10, 30, "Test 1");

i had no errors but no PDF was created..

i've also tried with this other code (Declarated QPdfWriter and QPainter):

QFile f("AAA.pdf");

f.open(QIODevice::WriteOnly);

QPdfWriter* writer = new QPdfWriter(&f);

writer->setPageSize(QPagedPaintDevice::A4);

QPainter* p = new QPainter(writer);

p->drawText(QRect(100, 100, 2000, 200), "test");

writer->newPage();

p->drawText(QRect(100, 100, 2000, 200), "test");

delete p;

delete writer;

f.close();

No errors but..

No way.. PDF was not created!

Anyone can tell me where i fail??


1 Respuesta
 
0



1 de 26


https://forum.qt.io/topic/96672/create-a-pdf-from-qt-creator 1/10
7/9/2021 Create a PDF from Qt Creator | Qt Forum

 
Accede para responder (/login)

(/user/jsulm)
 jsulm (/user/jsulm)
 LIFETIME QT CHAMPION (/groups/lifetime-qt-champion)
16 nov 2018 9:30 (/post/493508)

@TheCipo76 (https://forum.qt.io/uid/43214) Are you sure the PDF file was not created? You're using a relative path,
that means the file will be created in current working directory.


1 Respuesta
 
0

(/user/asperamanca)
 Asperamanca (/user/asperamanca)
16 nov 2018 9:36 (/post/493509)

@TheCipo76 (https://forum.qt.io/uid/43214) said in Create a PDF from Qt Creator (/post/493499):

setOutputFileName

Interesting. I use the QPrinter way, and the only difference to my code I spotted was that I explicitly set the page
margins and resolution. Also, I assume that you open the file for writing in your first code just as you do it in the
second one, right?

But try out the Text Edit example (in widgets\richtext) - it contains a PDF export.


1 Respuesta
 
0

(/user/thecipo76)
 TheCipo76 (/user/thecipo76)
16 nov 2018 10:09 (/post/493524)

@jsulm (https://forum.qt.io/uid/23827) i've searhced with finder and this is the result

(https://ddgobkiprc33d.cloudfront.net/07417895-0fce-45fd-b5ef-821a989ada49.png)


0



1 de 26


https://forum.qt.io/topic/96672/create-a-pdf-from-qt-creator 2/10
7/9/2021 Create a PDF from Qt Creator | Qt Forum

(/user/thecipo76)
 TheCipo76 (/user/thecipo76)
16 nov 2018 10:13 (/post/493525)

@Asperamanca (https://forum.qt.io/uid/4934)

No, i've followed a Video Tutorial and copy the same code

(https://www.youtube.com/watch?v=lA5D8b_QPxo (https://www.youtube.com/watch?v=lA5D8b_QPxo))

the only differences is that i've done it in a Dialog and not in MainWindows

i've already tried to create a new project and do it in MainWindows but the result is the same..

No PDf was Created



2 Respuestas
 
0

(/user/mrjj)
 mrjj (/user/mrjj)
 LIFETIME QT CHAMPION (/groups/lifetime-qt-champion)
16 nov 2018 10:17 (/post/493526)

@TheCipo76 (https://forum.qt.io/uid/43214)

Hi

Code does look fine.

Could you try this sample ?

https://wiki.qt.io/Exporting_a_document_to_PDF (https://wiki.qt.io/Exporting_a_document_to_PDF)

and see if you get pdf from that ?


1 Respuesta
 
0

(/user/thecipo76)
 TheCipo76 (/user/thecipo76)
16 nov 2018 10:28 (/post/493531)

@mrjj (https://forum.qt.io/uid/18260) Yes,

with this code PDF was created


0

(/user/thecipo76)
 TheCipo76 (/user/thecipo76)
16 nov 2018 13:46 (/post/493576)

@TheCipo76 (https://forum.qt.io/uid/43214)

when i launch pdf print in Application Output i saw this message:

QPainter::begin(): Returned false

QPainter::setPen: Painter not active

QPainter::end: Painter not active, aborted

Any suggestion to fix it??


1 Respuesta
 
0

(/user/mrjj)
 mrjj (/user/mrjj)
 LIFETIME QT CHAMPION (/groups/lifetime-qt-champion)
16 nov 2018 14:12 (/post/493582)

@TheCipo76 (https://forum.qt.io/uid/43214)

Hi

My guess would be that printer object goes out of scope ?


1 Respuesta
 
0



1 de 26


https://forum.qt.io/topic/96672/create-a-pdf-from-qt-creator 3/10
7/9/2021 Create a PDF from Qt Creator | Qt Forum

(/user/thecipo76)
 TheCipo76 (/user/thecipo76)
16 nov 2018 14:20 (/post/493583)

@mrjj (https://forum.qt.io/uid/18260) i've tried with this code:

qreal top=20, left=15, right=15, bottom=20;

QPrinter printer(QPrinter::PrinterResolution);

QPrintDialog dialog(&printer, this);

if(dialog.exec() == QDialog::Accepted){

QPrinterInfo pinfo(printer);

qDebug() << "Printer valid: " << printer.isValid();

qDebug() << "Printer Name: " << printer.printerName();

qDebug() << "Printer program: " << printer.printProgram();

qDebug() << "Is printer null: " << pinfo.isNull();

qDebug() << "Printer State: " << pinfo.state();

qDebug() << "Is printer default: " << pinfo.isDefault();

qDebug() << "Is printer remote: " << pinfo.isRemote();

and this is the result:

Printer valid: true

Printer Name: "Samsung_CLP_620_Series__SEC001599489B1A_"

Printer program: ""

Is printer null: false

Printer State: 0

Is printer default: true

Is printer remote: false

Printer seem to be OK

now the previous message about QPainter were not showed..


1 Respuesta
 
0

(/user/mrjj)
 mrjj (/user/mrjj)
 LIFETIME QT CHAMPION (/groups/lifetime-qt-champion)
16 nov 2018 14:23 (/post/493585)

@TheCipo76 (https://forum.qt.io/uid/43214)

That looks very ok.

But no PDF (PrimoPDF.pdf) is produced ?

Have a look at this code. it prints screen shots.

Looks very similar to yours.

void printv2(QPixmap& pix) {

QPrinter printer(QPrinter::HighResolution);

printer.setOrientation(QPrinter::Landscape);

printer.setPaperSize(QPrinter::A4);

 
printer.setOutputFormat(QPrinter::PdfFormat);


1 de 26


https://forum.qt.io/topic/96672/create-a-pdf-from-qt-creator 4/10
7/9/2021 Create a PDF from Qt Creator | Qt Forum
p p (Q );
printer.setOutputFileName("e:/test.pdf");

QPainter painter;

painter.begin(&printer);

double xscale = printer.pageRect().width() / double(pix.width());

double yscale = printer.pageRect().height() / double(pix.height());


1 Respuesta
 
0

(/user/thecipo76)
 TheCipo76 (/user/thecipo76)
16 nov 2018 14:28 (/post/493586)

@mrjj (https://forum.qt.io/uid/18260) No, "PrimoPDF.pdf" wasn't created..

the code you have posted is very similar..


1 Respuesta
 
0

(/user/mrjj)
 mrjj (/user/mrjj)
 LIFETIME QT CHAMPION (/groups/lifetime-qt-champion)
16 nov 2018 14:32 (/post/493588)

@TheCipo76 (https://forum.qt.io/uid/43214)

Yep and i get a e:/test.pdf so not really sure why you dont get any.

does it try to print to real printer ?

Can you try

void printv3() {

QPrinter printer(QPrinter::HighResolution);

printer.setOrientation(QPrinter::Landscape);

printer.setOutputFormat(QPrinter::PdfFormat);

printer.setPaperSize(QPrinter::A4);

printer.setOutputFileName("test.pdf");

QPainter painter;

painter.begin(&printer);

painter.drawText( 200,200, "HELLO" );

painter.end();

That give me a test.pdf in the build folder

 
(http://imagizer.imageshack.com/img924/8069/z29uDz.png)


1 de 26


https://forum.qt.io/topic/96672/create-a-pdf-from-qt-creator 5/10
7/9/2021 Create a PDF from Qt Creator | Qt Forum

and expected result

(http://imagizer.imageshack.com/img924/6228/743C0R.png)


1 Respuesta
 
0

(/user/thecipo76)
 TheCipo76 (/user/thecipo76)
16 nov 2018 14:44 (/post/493590)

@mrjj (https://forum.qt.io/uid/18260) i've tried with no luck

test.pdf was not created..


1 Respuesta
 
0

(/user/mrjj)
 mrjj (/user/mrjj)
 LIFETIME QT CHAMPION (/groups/lifetime-qt-champion)
16 nov 2018 14:48 (/post/493592)

@TheCipo76 (https://forum.qt.io/uid/43214)

Really odd. I cant guess sorry. Sample works here.

You use the code directly as is ?


1 Respuesta
 
0

(/user/thecipo76)
 TheCipo76 (/user/thecipo76)
16 nov 2018 14:49 (/post/493593)

@mrjj (https://forum.qt.io/uid/18260) Yes, copied and pasted

QPrinter printer(QPrinter::HighResolution);

printer.setOrientation(QPrinter::Landscape);

printer.setOutputFormat(QPrinter::PdfFormat);

printer.setPaperSize(QPrinter::A4);

printer.setOutputFileName("test.pdf");

QPainter painter;

painter.begin(&printer);

painter.drawText( 200,200, "HELLO" );

painter.end();


0



1 de 26


https://forum.qt.io/topic/96672/create-a-pdf-from-qt-creator 6/10
7/9/2021 Create a PDF from Qt Creator | Qt Forum

(/user/mrjj)
 mrjj (/user/mrjj)
 LIFETIME QT CHAMPION (/groups/lifetime-qt-champion)
16 nov 2018 14:50 (/post/493595)

and you also searched for test.pdf to make sure

its not created any where else ?


1 Respuesta
 
0

(/user/thecipo76)
 TheCipo76 (/user/thecipo76)
16 nov 2018 14:51 (/post/493596)

@mrjj (https://forum.qt.io/uid/18260)

(https://ddgobkiprc33d.cloudfront.net/d3f65aa3-087a-44b4-a02d-40c6fcbca320.png)


1 Respuesta
 
0

(/user/mrjj)
 mrjj (/user/mrjj)
 LIFETIME QT CHAMPION (/groups/lifetime-qt-champion)
16 nov 2018 14:55 (/post/493597)

@TheCipo76 (https://forum.qt.io/uid/43214)

Very odd.

Can you try this code. it let u select save location

if that dont work. something up with your Qt.

void printv4() {

QPrinter printer(QPrinter::HighResolution);

printer.setOrientation(QPrinter::Landscape);


 

1 de 26

printer.setOutputFormat(QPrinter::PdfFormat);


https://forum.qt.io/topic/96672/create-a-pdf-from-qt-creator 7/10
7/9/2021 Create a PDF from Qt Creator | Qt Forum
printer.setPaperSize(QPrinter::A4);

QString fileName = QFileDialog::getSaveFileName(0,"file", "c:\\", "pdf (*.pdf)");

printer.setOutputFileName(fileName);

QPainter painter;

painter.begin(&printer);

painter.drawText( 200,200, "HELLO" );

i d()


1 Respuesta
 
2

(/user/thecipo76)
 TheCipo76 (/user/thecipo76)
16 nov 2018 14:59 (/post/493598)

@mrjj (https://forum.qt.io/uid/18260)

(https://ddgobkiprc33d.cloudfront.net/2064bfda-f9c3-49ac-88cf-a15dd9380674.png)

(https://ddgobkiprc33d.cloudfront.net/6635a87c-3a15-4e38-9796-365956d80408.png)

Finally works!!!!!! Great!!!

I don't know why but Works!!!!

Thank you very much!!


1 Respuesta
 
1

(/user/thecipo76)   

TheCipo76 (/user/thecipo76)
16
12018
nov de 26
 

15:02 (/post/493599)
https://forum.qt.io/topic/96672/create-a-pdf-from-qt-creator 8/10
7/9/2021 Create a PDF from Qt Creator | Qt Forum

¡Esta publicación está eliminada!

(/user/mrjj)
 mrjj (/user/mrjj)
 LIFETIME QT CHAMPION (/groups/lifetime-qt-champion)
16 nov 2018 15:06 (/post/493600)

Super :)

So on mac it seems the file go somewhere else

if no path. maybe a read only place.

So when we tell the exact location , it does work :)


1 Respuesta
 
0

(/user/mpergand)
 mpergand (/user/mpergand)
16 nov 2018 17:30 (/post/493610)

@TheCipo76 (https://forum.qt.io/uid/43214) said in Create a PDF from Qt Creator (/post/493583):

printer.setOutputFileName("PrimoPDF.pdf");

When launched from QtCreator, the default working directory is set to:

MyApp/Contents/MacOS/

When launched from the finder, the path is usely the user directory, but actualy it is the root directory (/) and
obviously nothing is written (wrong permissions)


2

(/user/j-hilk)
 J.Hilk (/user/j-hilk)
 MODERATORS (/groups/moderators)
16 nov 2018 18:06 (/post/493615)

@mrjj (https://forum.qt.io/uid/18260) said in Create a PDF from Qt Creator (/post/493600):

Super :)

So on mac it seems the file go somewhere else if no path

IIRC the standard path is

/Users/yourUserName/Library/Application Support


0

(/user/dan1973)
 dan1973 (/user/dan1973)
27 mar 2021 7:47 (/post/651837)

Hi,

I was following your comments and conversation. I need to know how to draw lines in PDF document using Qt.


1 Respuesta
 
0

(/user/jsulm)
 jsulm (/user/jsulm)
 LIFETIME QT CHAMPION (/groups/lifetime-qt-champion)
29 mar 2021 1:57 (/post/652022)

@dan1973 (https://forum.qt.io/uid/61107) said in Create a PDF from Qt Creator (/post/651837):




1 de 26


https://forum.qt.io/topic/96672/create-a-pdf-from-qt-creator 9/10
7/9/2021 Create a PDF from Qt Creator | Qt Forum

I need to know how to draw lines in PDF document using Qt

https://doc.qt.io/qt-5/qpainter.html#drawLine-2 (https://doc.qt.io/qt-5/qpainter.html#drawLine-2)


1

 
Accede para responder (/login)



1 de 26


https://forum.qt.io/topic/96672/create-a-pdf-from-qt-creator 10/10

You might also like