Auto generate PDF's
-
Hello Everyone,
I need to auto-generate a PDF within my program, then send it via email. I have a 3rd party software I can use to send the emails, but to use it, I first need to create PDF without leaving my CPP program.
Currently, we do most of our report writing through Crystal...so I'm not sure quite where to start on this.
Thanks,
Scott
-
@scott-krise
hi,
Qt has the https://doc.qt.io/qt-5/qpdfwriter.html class,
that should have everything you need to create across platforms PDF's -
Thats great!! Thanks so much!
-
Looks like that is only available in qt-5 and up? We are currently in 4.4.3. I know we need to update...just haven't yet. What are my options with what we currently have?
-
@scott-krise I was going to suggest using Poppler library, but it looks like it's also tied to Qt 5...
-
@scott-krise
For Qt 4, the only way I know might work is: does theQPrinter
support print-to-PDF-and-save-to-file? In code I upgraded from 4 to 5 I think that's how they were doing save PDF from Qt. -
Pretty sure Qt4.7 had the PDF support in the Print Dialog..
Or you could try..
void AppData::ConvertToPDF( QString thefile ) { // load a place holder image and output in .pdf format // // input: filename of what to convert // output: input file converted to PDF // // NOTE: you may need to experiment with output canvas dimensions // tested with .jpg and .png // #if DEBUG_PDF qDebug() << "*AppData::ConvertToPDF:"; #endif // initialize a source and destination path..different on iOS, Android, and Desktop //QString docsPath = QStandardPaths::writableLocation(QStandardPaths::DocumentsLocation); //if ( ! docsPath.endsWith( "/" ) ) docsPath += "/"; //QPixmap canvas( docsPath + "yourfile.png", nullptr, Qt::AutoColor ); QString docsPath = ur->docpath; // read the graphics file into your "image canvas" //QPixmap canvas( docsPath + thefile, nullptr, Qt::AutoColor ); QImage canvas( docsPath + thefile ); // change image size to something else ?? // NOTE: 2400 assumes ~8.5x11 @ 300dpi with margins QImage scaled = canvas.scaledToWidth( 2400 ); // do the printer setup QPrinter printer; QPainter painter; // arbitrary output file name and page format printer.setOutputFileName( docsPath + "test.PDF" ); printer.setOutputFormat( QPrinter::PdfFormat ); printer.setResolution( 300 ); //printer.setPageMargins( 0.1, 0.1, 0.1, 0.1, QPrinter::Inch ); //printer.setPageMargins( 2.54, 2.54, 2.54, 2.54, QPrinter::Millimeter); printer.setPaperSize(QPrinter::A4); //printer.setPaperSize( QPrinter::Letter ); //printer.setFullPage( true ); // output it to the printer painter.begin( &printer ); //painter.drawPixmap( 0, 0, canvas ); //painter.drawImage( 0, 0, canvas ); painter.drawImage( 0, 0, scaled ); painter.end(); }
-
Hi,
That's because you are on macOS. The system natively supports printing in a PDF.