Results 1 to 10 of 10

Thread: Difficulties in Making PDF files in Mac PCs

  1. #1
    Join Date
    Apr 2012
    Posts
    17
    Thanks
    1
    Qt products
    Qt4
    Platforms
    MacOS X Windows

    Default Difficulties in Making PDF files in Mac PCs

    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?

    Qt Code:
    1. #include "mainwindow.h"
    2. #include <QPainter>
    3. #include <QPaintEvent>
    4. #include <QPrinter>
    5. #include <QMessageBox>
    6.  
    7. MainWindow::MainWindow(QWidget *parent) :
    8. QMainWindow(parent)
    9. {
    10. btn = new QPushButton("doPdf", this);
    11. move(0,0);
    12. btn->adjustSize();
    13. btn->move(100,15);
    14. connect(btn,SIGNAL(clicked()),this,SLOT(btnClicked()));
    15. }
    16.  
    17. MainWindow::~MainWindow()
    18. {
    19. }
    20. void MainWindow::btnClicked(){
    21. bool ok;
    22. QRect r=QRect(10,10,80,50);
    23. QPrinter printer;
    24. QPainter painter;
    25. printer.setOutputFileName("print.pdf");
    26. ok=printer.isValid();
    27. ok=painter.begin(&printer);
    28. if(ok)
    29. painter.drawRect(r);
    30. ok=ok&&painter.end();
    31. if(ok)
    32. QMessageBox::information(this,"","image printed on \"print.pdf\"");
    33. else
    34. QMessageBox::information(this,"","Some error occurred!");
    35. }
    36. void MainWindow::paintEvent(QPaintEvent * ev){
    37. QRect r=QRect(10,10,80,50);
    38. QPainter painter(this);
    39. painter.drawRect(r);
    40. }
    To copy to clipboard, switch view to plain text mode 

  2. #2
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: Difficulties in Making PDF files in Mac PCs

    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/qt...yment-mac.html

  3. #3
    Join Date
    Apr 2012
    Posts
    17
    Thanks
    1
    Qt products
    Qt4
    Platforms
    MacOS X Windows

    Default Re: Difficulties in Making PDF files in Mac PCs

    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

  4. #4
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: Difficulties in Making PDF files in Mac PCs

    Does the behaviour change if your code explicitly calls setOutputFormat()?

  5. #5
    Join Date
    Jun 2006
    Posts
    81
    Thanks
    6
    Thanked 5 Times in 4 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Difficulties in Making PDF files in Mac PCs

    Quote Originally Posted by ceraolo View Post
    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?

  6. #6
    Join Date
    Apr 2012
    Posts
    17
    Thanks
    1
    Qt products
    Qt4
    Platforms
    MacOS X Windows

    Default Re: Difficulties in Making PDF files in Mac PCs

    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.

  7. #7
    Join Date
    Jun 2006
    Posts
    81
    Thanks
    6
    Thanked 5 Times in 4 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Difficulties in Making PDF files in Mac PCs

    Quote Originally Posted by ceraolo View Post
    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, 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.

    Quote Originally Posted by ceraolo View Post
    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.

    Quote Originally Posted by ceraolo View Post
    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.

    Quote Originally Posted by ceraolo View Post
    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...

  8. #8
    Join Date
    Apr 2012
    Posts
    17
    Thanks
    1
    Qt products
    Qt4
    Platforms
    MacOS X Windows

    Default Re: Difficulties in Making PDF files in Mac PCs

    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
    Last edited by ceraolo; 9th April 2014 at 19:38.

  9. #9
    Join Date
    Jun 2006
    Posts
    81
    Thanks
    6
    Thanked 5 Times in 4 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Difficulties in Making PDF files in Mac PCs

    Quote Originally Posted by ceraolo View Post
    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.

  10. #10
    Join Date
    Apr 2012
    Posts
    17
    Thanks
    1
    Qt products
    Qt4
    Platforms
    MacOS X Windows

    Default Re: Difficulties in Making PDF files in Mac PCs

    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

Similar Threads

  1. Difficulties compiling qt statically
    By tom989 in forum Installation and Deployment
    Replies: 1
    Last Post: 17th November 2012, 00:38
  2. Replies: 1
    Last Post: 17th April 2010, 00:12
  3. Difficulties with QTableView and setModel
    By sekatsim in forum Qt Programming
    Replies: 11
    Last Post: 19th June 2008, 07:05
  4. making the printer correctly interpret pcl files
    By momesana in forum Qt Programming
    Replies: 1
    Last Post: 17th April 2008, 16:41
  5. QTableView header difficulties
    By croftj in forum Qt Programming
    Replies: 6
    Last Post: 30th January 2008, 20:58

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.