printer_.setPrinterName("Canon_PRO_100_series");
printer_.setResolution(300);
printer_.setPageLayout(page_layout);
printer_.setCopyCount(1);
printer_.setDoubleSidedPrinting(false);
printer_.
setDuplex(QPrinter::DuplexNone);
printer_.
setPaperSize(QPrinter::Letter);
printer_.
setPaperSource(QPrinter::Auto);
printer_.setCreator("Inkjet Plumber");
printer_.
setOrientation(QPrinter::Portrait);
printer_.
setPageMargins(.5, .5, .5, .5,
QPrinter::Inch);
printer_.setPageLayout(page_layout);
printer_.setDocName("Inkjet Plumber Maintenance Job");
// printer_.setOutputFileName("/Users/jefft/Downloads/ijp.pdf");
// printer_.setOutputFormat(QPrinter::PdfFormat);
printer_.
setOutputFormat(QPrinter::NativeFormat);
if (!printer_.isValid())
qDebug("Printer is invalid!");
QPrinter printer_(QPrinter::HighResolution);
printer_.setPrinterName("Canon_PRO_100_series");
printer_.setResolution(300);
printer_.setPageLayout(page_layout);
printer_.setCopyCount(1);
printer_.setDoubleSidedPrinting(false);
printer_.setDuplex(QPrinter::DuplexNone);
printer_.setColorMode(QPrinter::Color);
printer_.setPageSize(QPrinter::Letter);
printer_.setPaperSize(QPrinter::Letter);
printer_.setPaperSource(QPrinter::Auto);
printer_.setCreator("Inkjet Plumber");
printer_.setOrientation(QPrinter::Portrait);
printer_.setPageMargins(.5, .5, .5, .5, QPrinter::Inch);
printer_.setPageLayout(page_layout);
printer_.setDocName("Inkjet Plumber Maintenance Job");
// printer_.setOutputFileName("/Users/jefft/Downloads/ijp.pdf");
// printer_.setOutputFormat(QPrinter::PdfFormat);
printer_.setOutputFormat(QPrinter::NativeFormat);
if (!printer_.isValid())
qDebug("Printer is invalid!");
To copy to clipboard, switch view to plain text mode
If I uncomment lines 16-17 and print to PDF, the PDF file looks as expected and prints fine using the Mac Preview app. The code I use to render the page is:
void MainWindow
::paint_page(QPrinter *printer
) {
painter.begin(printer);
painter.
setRenderHint(QPainter::Antialiasing);
painter.setPen(Qt::black);
// Draw page rect to show printable page (excluding margins)
// painter.drawRect(painter_rect);
painter.
setFont(QFont("Tahoma",
10));
painter.
drawText(0,
0,
"Inkjet Plumber maintenance job sent to " + printer_info_.
printerName() + ": " + QDateTime::currentDateTime().
toString("yyyy-MM-dd hh:mm:ss."));
painter.
setPen(QPen(Qt
::black));
print_swatch(painter, 0, 50, Qt::cyan);
print_swatch(painter, 200, 50, Qt::magenta);
print_swatch(painter, 400, 50, Qt::yellow);
print_swatch(painter, 600, 50, Qt::black);
print_swatch(painter, 800, 50, Qt::gray);
print_swatch(painter, 1000, 50, Qt::lightGray);
print_swatch(painter, 1200, 50, Qt::red);
print_swatch(painter, 1400, 50, Qt::green);
print_swatch(painter, 1600, 50, Qt::blue);
painter.end();
return;
}
void MainWindow::paint_page(QPrinter *printer)
{
QPainter painter;
painter.begin(printer);
painter.setRenderHint(QPainter::Antialiasing);
painter.setPen(Qt::black);
// Draw page rect to show printable page (excluding margins)
// painter.drawRect(painter_rect);
painter.setFont(QFont("Tahoma", 10));
painter.drawText(0, 0, "Inkjet Plumber maintenance job sent to " + printer_info_.printerName() + ": " + QDateTime::currentDateTime().toString("yyyy-MM-dd hh:mm:ss."));
painter.setPen(QPen(Qt::black));
print_swatch(painter, 0, 50, Qt::cyan);
print_swatch(painter, 200, 50, Qt::magenta);
print_swatch(painter, 400, 50, Qt::yellow);
print_swatch(painter, 600, 50, Qt::black);
print_swatch(painter, 800, 50, Qt::gray);
print_swatch(painter, 1000, 50, Qt::lightGray);
print_swatch(painter, 1200, 50, Qt::red);
print_swatch(painter, 1400, 50, Qt::green);
print_swatch(painter, 1600, 50, Qt::blue);
painter.end();
return;
}
To copy to clipboard, switch view to plain text mode
Here's the print preview code that works fine and prints fine if I print using the QPrintPreviewDialog:
QPrintPreviewDialog preview_dlg(&printer_);
connect(&preview_dlg, &QPrintPreviewDialog::paintRequested, this, &MainWindow::paint_page);
int result = preview_dlg.exec();
QPrintPreviewDialog preview_dlg(&printer_);
connect(&preview_dlg, &QPrintPreviewDialog::paintRequested, this, &MainWindow::paint_page);
int result = preview_dlg.exec();
To copy to clipboard, switch view to plain text mode
and finally the QPrintDialog snippet that works fine as well, but this program is a utility that I need to print w/o user intervention, so the QPrintDialog isn't really an option:
Bookmarks