PDA

View Full Version : Difficulties in Making PDF files in Mac PCs



ceraolo
5th April 2014, 17:53
Printing pdf's is normally straightforward in Qt.

However there are issues under Mac PC's.

Take the code below.
It does work perfectly under Windows: clicking on the button creates a pdf file containing the small rectangle at the top left part of the page. The message "image printed on "print.pdf" " is displayed.

If I compile the same code under Mac and run the program from Qt creator, again I get a correct pdf.
But if I run the .app created by Qt, both "as is" and "fed up" using the macdeployqt utility, the painter does not construct and the message "Some error occurred" is displayed.

Any suggestions on how to overcome this?



#include "mainwindow.h"
#include <QPainter>
#include <QPaintEvent>
#include <QPrinter>
#include <QMessageBox>

MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent)
{
btn = new QPushButton("doPdf", this);
move(0,0);
btn->adjustSize();
btn->move(100,15);
connect(btn,SIGNAL(clicked()),this,SLOT(btnClicked ()));
}

MainWindow::~MainWindow()
{
}
void MainWindow::btnClicked(){
bool ok;
QRect r=QRect(10,10,80,50);
QPrinter printer;
QPainter painter;
printer.setOutputFileName("print.pdf");
ok=printer.isValid();
ok=painter.begin(&printer);
if(ok)
painter.drawRect(r);
ok=ok&&painter.end();
if(ok)
QMessageBox::information(this,"","image printed on \"print.pdf\"");
else
QMessageBox::information(this,"","Some error occurred!");
}
void MainWindow::paintEvent(QPaintEvent * ev){
QRect r=QRect(10,10,80,50);
QPainter painter(this);
painter.drawRect(r);
}

ChrisW67
5th April 2014, 22:48
Have you created an application bundle with all the necessary Qt libraries and plugins or are you relying on some shared Qt framework?

https://qt-project.org/doc/qt-5.0/qtdoc/deployment-mac.html

ceraolo
7th April 2014, 00:49
I've created the bundle using the macdeployqt utility, that should have added everything needed.
In fact, if I have a look at the bundle contents I see that in the Frameworks folder there are:
QtCore.framework
QtGui.framework
QtPrintSupport.framework
QtWidgets.framework

Moreover the issue must not be related to the addition of frameworks to the bundle, since the bundle before being modified using macdeployqt utility already starts.
This means that it is able to find on my mac Air the needed libraries for its basic functions, such as creating the window, since in this PC Qt is installed.
The macdeployqt utility, I think, should be needed only to run the application in PCs in which Qt is not installed.

So, the reason for which printing on PDF does not work is mysterious for me.
I can add that if I use the code similar of my post, but without row 25, printing on the physical printer on my mac (instead of the PDF) is performed correctly.

Regards

ChrisW67
7th April 2014, 02:30
Does the behaviour change if your code explicitly calls setOutputFormat()?

zaphod.b
7th April 2014, 14:08
But if I run the .app created by Qt, both "as is" and "fed up" using the macdeployqt utility, the painter does not construct and the message "Some error occurred" is displayed.


Have you double-checked all lookup paths with otool, and eventually adjusted with install_name_tool?

ceraolo
7th April 2014, 22:35
ChrisW67, after you hint I tried, but with no success.

Zaphod, I don't know neither tool nor install_name_tool. But inside the app, after macdeployqt, as I said, I see all the needed frameworks. Moreover, they are all correctly accessed, even QtPrintSupport.framework (in fact on a physical printer my tiny program prints).
I'm becoming convinced that there must be some bug out there.

zaphod.b
8th April 2014, 09:59
Zaphod, I don't know neither tool nor install_name_tool.

These tools are explained, or at least shown how to put to use, in Deploying an Application on Mac OS X (http://qt-project.org/doc/qt-4.8/deployment-mac.html), as is macdeployqt. With otool you can check which paths the dynamic linker will use to reference dylibs. With install_name_tool you can adjust the ids on both ends of these references.



But inside the app, after macdeployqt, as I said, I see all the needed frameworks.

To see the frameworks is not enough. The references must fit. Usually this is ok for the Qt frameworks after macdeployqt, but sometimes it's not, and your own libs you will almost certainly have to fix.



Moreover, they are all correctly accessed, even QtPrintSupport.framework (in fact on a physical printer my tiny program prints).

When you run the bundle? Hmm this renders my notes useless then.



I'm becoming convinced that there must be some bug out there.

When I familiarized myself with deployment on Mac i ran into severe cases of PEBKAC with the exact same symptoms as yours more than once... :rolleyes:

ceraolo
9th April 2014, 20:38
Thank you of all your advice, zaphod.
I will try something from your hints next weekend (programming is not my job).
I will post something after this.

MC

Added after 1 14 minutes:

Well, it is a real bug!
I've found a ticket open here:
https://bugreports.qt-project.org/browse/QTBUG-34404

I've added my experience to that ticket.

MC

zaphod.b
10th April 2014, 11:59
Well, it is a real bug!
I've found a ticket open here:
https://bugreports.qt-project.org/browse/QTBUG-34404

I've added my experience to that ticket.


Good to know. Might become relevant to me in the foreseeable future. Thx for your feedback.

ceraolo
17th April 2014, 17:45
Well, indeed that ticket did not apply well to my case.
My issue was due to the fact that my code tried to open "print.pdf" without any specification of path.
When run from QT creator, since I specified as Working directory /Users/ceraolo/Documents (i.e. a directory to which I have allo rights to write) pdf creation was successful.
When I ran my bundle from Mac's Finder, it tried to open the pdf file in "Macintosh HD", where I don't have rights to write.

If I specify a full pathname for the output pdf, referring to a directory I have access to write, everything works, even from Finder!

MC