QPrinter's PDF data, how to grab it? - c++

As the title says, how do i grab the internal PDF data from QPrinter without going the extra mile of outputting to a temporary file and reading that in for further processing?
I have checked the documentation and found nothing what would let me do something like
QPrinter printer(QPrinter::HighResolution);
printer.setPageSize(QPrinter::A4);
printer.setOutputFormat(QPrinter::PdfFormat);
QTextDocument doc;
doc.setHtml("<p>Test me!</p>");
doc.print(&printer);
QByteArray foo = printer.data();
Any ideas? :)

The only way I know would be by using QTemporaryFile
QPrinter printer(QPrinter::HighResolution);
printer.setPageSize(QPrinter::A4);
printer.setOutputFormat(QPrinter::PdfFormat);
QTemporaryFile pdf;
pdf.open();
printer.setOutputFileName(pdf.fileName());
QTextDocument doc;
doc.setHtml("<p>Test me!</p>");
doc.print(&printer);
QByteArray foo = pdf.readAll();

Related

File Not Being Created in QT C++ using QFileDialog

there I'm trying to make a simple notepad application. I've got a new file menu item. When the user clicks on that, it checks whether the text field is empty or not. If that is not empty then it prompts to save the work. If the user opts for yes then it asks for the location using QFileDialog. However, this code doesn't create the file at the provided destination. Can anyone figure out this code?
if(this->checkTextField()==false){ //checkks whether the textField is empty or not
QMessageBox::StandardButton reply = QMessageBox::question(this,this->appName,this->document_modified);
if(reply == QMessageBox::Yes){
QString filename = QFileDialog::getSaveFileName(this,"Save the File",QDir::homePath(),this->textFilter);
QFile file(filename);
QTextStream out(&file);
QString text = ui->textEdit->toPlainText();
out << text;
file.flush();
file.close();
}
}
Simply creating a QFile will neither create nor open a file on the filesystem. You have to open it to write something to it/read from it.

Print a PDF file with Qt

I know that there are similar questions about this issues but not a single one seems to address my problem.
I have two options In my widget: one that allows the user to save the display as PDF file and one that allows the user to print the display directly.
My problem is that there seems to be quite some minor differences if I print the saved PDF file with Windows vs when I print the interface with Qt. A possible solution that I begin to follow is that when I print the interface first to save the interface in PDF as a temporary file and after to try to print with Qt that file.
Unfortunately it seems that I can not use directly the PDF file without convert it in a pixmap or something similar.
This is how I manage to export in PDF format:
QTextDocument doc;
if (!ComputeDoc(doc))
return false;
QPrinter printer;
printer.setOutputFileName(filePath);
printer.setPaperSize(format);
printer.setOutputFormat(QPrinter::PdfFormat);
printer.setOrientation (orientation);
printer.setFullPage(true);
if(ok && !filePath.isEmpty())
{
QPainter painter(&printer);
painter.setBackgroundMode(Qt::TransparentMode);
painter.setRenderHint(
(QPainter::RenderHint)(int)(
QPainter::Antialiasing |
QPainter::TextAntialiasing |
QPainter::HighQualityAntialiasing |
QPainter::SmoothPixmapTransform),
true);
painter.setPen(Qt::white);
doc.drawContents(&painter);
}
And this how I originally try to print the file:
QTextDocument doc;
if (!ComputeDoc(doc))
return false;
QPrinter printer;
printer.setPaperSize(format);
printer.setOrientation (orientation);
printer.setFullPage(true);
QPrintDialog printDialog(&printer, this);
if(ok && printDialog.exec() == QDialog::Accepted)
{
QPainter painter(&printer);
painter.setBackgroundMode(Qt::TransparentMode);
painter.setRenderHint(
(QPainter::RenderHint)(int)(
QPainter::Antialiasing |
QPainter::TextAntialiasing |
QPainter::HighQualityAntialiasing |
QPainter::SmoothPixmapTransform),
true);
painter.setPen(Qt::white);
doc.drawContents(&painter);
}
As you can see from the print the inner frame is not centered and in the left part (the image is flipped to right) of the paper there is a line that in PDF is not. Sorry for this example but I can not post the entire page. I hope this will give a small clarification.
Does anyone know a solution to print a PDF file from the disk with qt? Or maybe how to make Windows to print to file from qt?

QPrinter output to pdf is not equivalent to the paper/page size (A4)

I am trying to output a QWidget ui to a pdf file like this;
const QString filename = "class1bills.pdf";
printer = new QPrinter(QPrinter::HighResolution);
printer->setOutputFormat(QPrinter::PdfFormat);
printer->setOrientation(QPrinter::Portrait);
printer->setPaperSize(QPrinter::A4);
printer->setPageSize(QPrinter::A4);
printer->setPageMargins (15,15,15,15,QPrinter::Millimeter);
printer->setFullPage(false);
printer->setOutputFileName(filename);
painter = new QPainter(printer);
class1Bill->render(painter, QPoint(), QRegion(), QWidget::DrawChildren | QWidget::DrawWindowBackground);
painter->begin(printer);
painter->end();
assert(QFile::exists(filename));
class1Bill is an object of the class that inherits QWidget. Everything works fine but when i open the pdf file, i expect the widget ui to appear fitting the A4 page size but it appears very small that i didn't even see it at first glance. How do i make the widget ui fit the A4 paper size i set?
Set printer->setFullPage(true); in order to fit paper size

Howto open html page in default browser, when html code is stored in QString

My Qt application should open a html page (with the default browser eg. IE). This html code is stored in a QString.
What would be the best way to open this "file", of which I only have the content?
Is QTemporaryFile the answer to this? Or could this be done easier?
QString content = "<html>...</html>";
?
QDesktopServices::openUrl(QUrl("..."));
The QTemporaryFile approach is by far the easiest to accomplish your task.
I don't see any other way other then doing some "vodoo" with ActiveQt, if that works at all.
Best regards.
EDIT: An example
QString htmlData; // your HTML data here
// The six Xs are actually required.
QTemporaryFile tmpFile( QLatin1String( "thefileXXXXXX.html" ) );
tmpFile.open();
QTextStream out( &tmpFile )
out << htmlData;
tmpFile.close();
QDesktopServives::openUrl( QUrl::fromLocalFile( tmpFile.fileName() ) );

How to extend QClipboard?

How can I extend QClipboard to allow selection of all text in all open windows.
Please provide code.
Thanks
See QClipboard it's very simple for just text
QClipboard *clipboard = QApplication::clipboard();
QString getCLipboardText = clipboard->text();
QString newClipboardText("blah blah");
clipboard->setText(newClipboardText);