Results 1 to 10 of 10

Thread: High quality business papers

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Join Date
    Oct 2006
    Location
    Hawaii
    Posts
    130
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows
    Thanks
    48
    Thanked 4 Times in 4 Posts

    Default Re: High quality business papers

    Heres what I do for HQ documents:

    1) I create a printer
    2) I use QPrinter::PdfFormat to make the printer print to PDF
    3) I make a QPainter with the QPrinter as the device to print to
    4) I then have programmed instructions to draw and type stuff with the QPainter on the printer,
    5) When I call painter.end(), it causes the PDF to be created in perfect quality.

    Although this method might be somewhat time consuming at first to set up, once you get the hang of it, you can make perfect documents that print on any printer exactly as you intend them to.

    Heres a very simple example code to make a pdf with some random lines and text (more of a test of how the page sizes work and how stuff fits) :
    Qt Code:
    1. void pdf::generatePdf() {
    2.  
    3. QPrinter printer; //create a printer
    4. printer.setOrientation(QPrinter::Portrait); //set the orientation of the paper
    5. printer.setOutputFormat(QPrinter::PdfFormat); //make that printer as a PDF
    6. printer.setOutputFileName("filename.pdf"); //set the PDF file name
    7. printer.setPaperSize(QPrinter::Letter); //set paper size that the PDF uses
    8. printer.setPageMargins (0.5, 0.5, 0.5, 0.5, QPrinter::Inch); //1/2 inch margins
    9. QPainter painter(&printer); //make a painter, which uses this printer,
    10.  
    11. //draw a blue round edged rectangle with the word "Hi" in it:
    12. painter.setPen(Qt::blue);
    13. painter.setFont(QFont("Arial", 30));
    14. QRectF r1(100, 200, 100, 100);
    15. painter.drawText(r1, Qt::AlignCenter, "Hi");
    16. painter.drawRoundedRect(r1, 15.0, 15.0);
    17.  
    18. //draw a black line from the upper left, to the lower right side of the page
    19. painter.setPen(Qt::black);
    20. QLineF line(0, 0, 576, 756);
    21. painter.drawLine(line);
    22.  
    23. //start a new page:
    24. printer.newPage();
    25.  
    26. //draw a red box that reads "hello!" in it:
    27. painter.setPen(Qt::red);
    28. painter.setFont(QFont("Arial", 30));
    29. QRectF r2(10, 30, 100, 100);
    30. painter.drawText(r2, Qt::AlignCenter, "Hello!");
    31. painter.drawRect(r2);
    32.  
    33. //draw a green dashed/dotted line in thick stroke, from upper right to lower left of page:
    34. QPen pen(Qt::green, 3, Qt::DashDotLine, Qt::RoundCap, Qt::RoundJoin);
    35. painter.setPen(pen);
    36. QLineF line2(0, 756, 576, 0);
    37. painter.drawLine(line);
    38.  
    39. //end the painter causing the PDF to be generated as "filename.pdf":
    40. painter.end(); //done drawing, so save the PDF
    41.  
    42. }
    To copy to clipboard, switch view to plain text mode 
    you can then play around with more lines, positions, images, getting text from a database, etc which you can add using the painter. You can also print rich text documents to PDF like this, there is a text editor example that comes with Qt and within there is code to convert rich text documents to PDF if they are already in that format.
    Last edited by tpf80; 14th October 2008 at 09:00.

  2. #2
    Join Date
    Apr 2008
    Posts
    196
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows
    Thanked 8 Times in 6 Posts
    Wiki edits
    1

    Default Re: High quality business papers

    @tpf80: I don't want to draw the lines by my own, i would like to draw the lines with the finished html file. But I also read in the article that XSL-T won't be included in the QT Version 4.5.

    I also looked to KD Reports for that problem, but the german phone number doesn't work. Did you know where I can have more informations about the price and/or licensing?

    Regards

  3. #3
    Join Date
    Oct 2006
    Location
    Hawaii
    Posts
    130
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows
    Thanks
    48
    Thanked 4 Times in 4 Posts

    Default Re: High quality business papers

    One way you could convert the HTML straight to PDF would be like this:

    Qt Code:
    1. QPrinter printer; //create a printer
    2. printer.setOrientation(QPrinter::Portrait); //set the orientation of the paper
    3. printer.setOutputFormat(QPrinter::PdfFormat); //make that printer as a PDF
    4. printer.setOutputFileName("filename.pdf"); //set the PDF file name
    5. QTextDocument *document = new QTextDocument();
    6. document->setDefaultStyleSheet(styleSheet); //the style sheet QString;
    7. document->setHtml(htmlContent); //the HTML QString;
    8. document->print(&printer);
    To copy to clipboard, switch view to plain text mode 

    Note that the stylesheet is loaded separately from the html. Here is a list of the tags which the QTextDocmument can understand: http://doc.trolltech.com/4.4/richtext-html-subset.html

  4. #4
    Join Date
    Apr 2008
    Posts
    196
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows
    Thanked 8 Times in 6 Posts
    Wiki edits
    1

    Default Re: High quality business papers

    Thanks,

    i will try it. Must the style sheet also contain the <style> and </style> tags?

    Regards

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
  •  
Qt is a trademark of The Qt Company.