void Document::exportPDF(QString fileName) { // QPdfWriter pdfWriter("/Users/tom/Desktop/qpdfwriter.pdf"); // QPdfWriter pdfWriter(fileName); QPrinter pdfWriter(QPrinter::HighResolution); pdfWriter.setOutputFormat(QPrinter::PdfFormat); pdfWriter.setOutputFileName(fileName); // pdfWriter.setMargins(); pdfWriter.setPageSize(QPageSize(QSizeF(pages[0].width(), pages[0].height()), QPageSize::Point)); pdfWriter.setPageMargins(QMarginsF(0, 0, 0, 0)); qreal zoomW = ((qreal)pdfWriter.pageRect().width()) / ((qreal)pdfWriter.paperRect().width()); qreal zoomH = ((qreal)pdfWriter.pageRect().height()) / ((qreal)pdfWriter.paperRect().height()); qreal zoom = zoomW; if (zoomH < zoomW) zoom = zoomH; pdfWriter.setResolution(72); pdfWriter.pageLayout().setUnits(QPageLayout::Point); QPainter painter; // std::cout << "PDF " << pdfWriter.colorCount() << std::endl; painter.begin(&pdfWriter); painter.setRenderHint(QPainter::Antialiasing, true); for (int pageNum = 0; pageNum < pages.size(); ++pageNum) { if (pages[pageNum].backgroundColor() != QColor("white")) { QRectF pageRect = pdfWriter.pageRect(QPrinter::Point); pageRect.translate(-pageRect.topLeft()); painter.fillRect(pageRect, pages[pageNum].backgroundColor()); } pages[pageNum].paint(painter, zoom, QRect(0, 0, 0, 0)); if (pageNum + 1 < pages.size()) { pdfWriter.setPageSize(QPageSize(QSize(pages[pageNum + 1].width(), pages[pageNum + 1].height()))); pdfWriter.newPage(); } } painter.end(); }
void PDF::slotPrintButton() { QDir monRepertoire; bool firstpage = true; SeriesInfos.SetChapter(ui->StartChap->value()); monRepertoire.setPath(SeriesInfos.GetChapterFolder()); if (monRepertoire.exists()) { QPdfWriter pdfWriter(SeriesInfos.GetSeriesFolder() + "/" + SeriesInfos.GetSeriesName() + " Tome " + SeriesInfos.GetVolume() + ".pdf"); pdfWriter.setPageSize(QPagedPaintDevice::A4); const qreal horizontalMarginMM = 10.0; // 10 mm margin on each side const qreal verticalMarginMM = 10.0; QPagedPaintDevice::Margins margins; margins.left = margins.right = horizontalMarginMM; margins.top = margins.bottom = verticalMarginMM; pdfWriter.setMargins(margins); QPixmap pixmap; QPainter painter; while ((monRepertoire.exists()) && (ui->EndChap->value() >= SeriesInfos.GetChapter().toInt())) { QStringList listOfFiles = monRepertoire.entryList(); for (int i = 2; i < listOfFiles.size(); i++) { QString imagePath = monRepertoire.absolutePath() + "/" + listOfFiles.at(i); if ((imagePath.contains("jpg")) || (imagePath.contains("png"))) { pixmap.load(imagePath); if (pixmap.width() > pixmap.height()) { if (ui->Vertical->isChecked()) { if (firstpage == false) { pdfWriter.newPage(); } else { painter.begin(&pdfWriter); } QTransform t; pixmap = pixmap.transformed(t.rotate(90),Qt::SmoothTransformation); painter.drawPixmap(QRectF(0, 0, pdfWriter.width(), pdfWriter.height()), pixmap, QRectF(0, 0, pixmap.width(), pixmap.height())); } else { pdfWriter.setPageOrientation(QPageLayout::Landscape); if (firstpage == false) { pdfWriter.newPage(); } else { painter.begin(&pdfWriter); } painter.drawPixmap(QRectF(0, 0, pdfWriter.width(), pdfWriter.height()), pixmap, QRectF(0, 0, pixmap.width(), pixmap.height())); pdfWriter.setPageOrientation(QPageLayout::Portrait); } } else { if (firstpage == false) { pdfWriter.newPage(); } else { painter.begin(&pdfWriter); } painter.drawPixmap(QRectF(0, 0, pdfWriter.width(), pdfWriter.height()), pixmap, QRectF(0, 0, pixmap.width(), pixmap.height())); } firstpage = false; } } SeriesInfos.UpdateChapterVal(); monRepertoire.setPath(SeriesInfos.GetChapterFolder()); } emit UpdateVolume(CurrentIdx); painter.end(); } this->close(); }