Create and write into a PDF File
-
I know this topic has been discussed a million times, but i dont understand why it isnt working for me.
I want to create and write into a PDF file, when i click on a button.void MainWindow::on_button_clicked()
{
QPrinter printer2;
printer2.setOutputFormat(QPrinter::PdfFormat);
printer2.setOutputFileName("test.pdf");
QPainter painter2;
painter2.drawText(10,10, "Test");
painter2.end();
}This is just one of many different approaches i tried.
The problem is, QT doesnt create any file.Thanks in advance!
-
Hi! Here's a working example.
-
@Eddy Yes.
@Wieland
Thanks for your answers.
I am not sure about the arguments in painter.drawText().
When i run the programm and press the button, QT says "QPainter::begin(): Returned false
Could you help me with that?void MainWindow::writePdf()
{
const QString filename("D://Programme/QT/MeineProjekte/Projekt/test.pdf");QString testData = "test"; QPdfWriter pdfwriter(filename); pdfwriter.setPageSize(QPageSize(QPageSize::A4)); QPainter painter(&pdfwriter); painter.drawText(0,0, testData);
}
-
@mrjj
thanks
I still having error.
This is my code
void MainWindow::on_pushButton_createpdf_clicked()
{
QPrintPreviewDialog dialog;
QPainter painter(this);
connect(&dialog, SIGNAL(paintRequested(QPrinter*)), this, SLOT(print(QPrinter*)));
dialog.exec();
painter.setPen(Qt::black);
painter.setFont(QFont("Arial", 30));
painter.drawText(rect(), Qt::AlignCenter, "Technological University of the Philipines - Manila");
painter.end();
painter.drawPixmap (C:\User\Downloads\logo.png);
} -
@drich said in Create and write into a PDF File:
painter.end();
painter.drawPixmap (C:\User\Downloads\logo.png);I guess
painter.end();
should be last line and
painter.drawPixmap (C:\User\Downloads\logo.png);
is not valid C++. Should be:
painter.drawPixmap ("C:/User/Downloads/logo.png");
-
@artwaw hi, I remove the painter.Pixmap and I run my program I clicked the createpdf button and it show a blank paper it didn't show this
painter.setPen(Qt::black);
painter.setFont(QFont("Arial", 30));
painter.drawText(rect(), Qt::AlignCenter, "Technological University of the Philipines - Manila"); -
@drich That means that you passed too few arguments to the method. Please refer to the documentation I linked in one of my previous posts - in that webpage together with a quite detailed overview on how to use QPainter there is also a list of the methods provided. You will see that QPainter::drawPixmap comes in many variants with different arguments list so you can choose the one that best suits your needs.
It seems to me that taking a look at Paint System would also bring you some significant benefits in terms of overall understanding how to accomplish what you aim for. -
@drich I think that is because you have not had supplied a valid rectangle area which tells QPainter::drawText() where to draw. Please refer to the documentation I linked above to learn how to draw on widgets. It would be more helpful and consistent than explaining every detail in here.