Handling PDF: Difference between revisions

From Qt Wiki
Jump to navigation Jump to search
No edit summary
 
No edit summary
Line 1: Line 1:
=Handling <span class="caps">PDF</span>=
[[Category:Developing_with_Qt]]<br />[toc align_right=&quot;yes&amp;quot; depth=&quot;2&amp;quot;]


This page discusses various available options for working with [http://en.wikipedia.org/wiki/Portable_Document_Format Portable Document Format (<span class="caps">PDF</span>)] ''[en.wikipedia.org]'' documents in your Qt application. Please also read the general considerations outlined on the [[Handling Document Formats]] page.
= Handling PDF =


''<font size="0.9em"><font color="#335">Note that this information is collaboratively collected by the community, with no promise of completeness or correctness. In particular, use your own research and judgment when evaluating third-party libraries or tools!</font></font>''
This page discusses various available options for working with &quot;Portable Document Format &amp;#40;PDF&amp;amp;#41;&quot;:http://en.wikipedia.org/wiki/Portable_Document_Format documents in your Qt application. Please also read the general considerations outlined on the [[Handling_Document_Formats | Handling Document Formats]] page.


==Reading / Writing==
p{width:60%;border:solid 1px #99a;background:#eef;color:#335;padding:2pt 4pt;font-size:0.9em;line-height:150%;font-style:italic}. Note that this information is collaboratively collected by the community, with no promise of completeness or correctness. In particular, use your own research and judgment when evaluating third-party libraries or tools!


===Using QPrinter===
== Reading / Writing ==


* '''''manual QPainter painting'''''<br /> The most basic (but not necessarily simplest) way of creating <span class="caps">PDF</span> documents with QPrinter is by manually painting the document’s content with Qt’s [[doc/qt-4.8/qt4-arthur.html|Arthur paint system]].<br /> Just pass the QPrinter object as a reference to the constructor of [[doc/QPainter|QPainter]] (or, alternatively, to [[doc/qt-4.8/qpainter.html#begin|QPainter::begin]] for an already existing QPainter) and then perform any painting operations with that QPainter instance like you usually would (with intermittent calls to [[doc/qt-4.8/qprinter.html#newPage|QPrinter::newPage]] whenever you want to move on to the next <span class="caps">PDF</span> page).
=== Using QPrinter ===


* '''''Scribe'''''<br /> For a more high-level <span class="caps">API</span> for creating structured rich-text documents, use Qt’s Scribe framework (see [[Handling Document Formats]]). You can export the whole document or a part of it to <span class="caps">PDF</span> with [[doc/qt-4.8/qtextdocument.html#print|QTextDocument::print]] or [[doc/qt-4.8/qtextedit.html#print|QTextEdit::print]] (again, using a QPrinter object set up as shown above).
For {color:#709}creating PDF documents from scratch, you can use Qt's built-in print support which also allows &quot;printing&amp;quot; to PDF files. To do so you can set up a [[Doc:QPrinter]] instance like this: <code>QPrinter printer(QPrinter::HighResolution);<br />printer.setOutputFormat(QPrinter::PdfFormat);<br />printer.setOutputFileName(&quot;path/to/file.pdf&amp;quot;);</code> Since QPrinter inherits [[Doc:QPaintDevice]], anything that supports outputting graphical content to a QPaintDevice (or has convenience API for printing with QPrinter) can thus be used for generating PDFs:


* '''''Graphics View'''''<br /> Qt’s [[doc/qt-4.8/graphicsview.html|Graphics View framework]] can be a more suitable alternative for creating <span class="caps">PDF</span> documents with content that is mainly based on arbitrarily positioned and transformed 2D graphical items rather than continuous flowed rich text.<br /> To export the content of a graphics scene or view (or a part of it) to <span class="caps">PDF</span>, you need to manually initialize a QPainter configured to paint on a <span class="caps">PDF</span>-creating QPrinter (as described above), and pass it to [[doc/qt-4.8/qgraphicsscene.html#render|QGraphicsScene::render]] or [[doc/qt-4.8/qgraphicsview.html#render|QGraphicsView::render]].
* '''''manual QPainter painting'''''<br />The most basic (but not necessarily simplest) way of creating PDF documents with QPrinter is by manually painting the document's content with Qt's &quot;Arthur paint system&amp;quot;:/doc/qt-4.8/qt4-arthur.html.<br />Just pass the QPrinter object as a reference to the constructor of [[Doc:QPainter]] (or, alternatively, to &quot;QPainter::begin&amp;quot;:/doc/qt-4.8/qpainter.html#begin for an already existing QPainter) and then perform any painting operations with that QPainter instance like you usually would (with intermittent calls to &quot;QPrinter::newPage&amp;quot;:/doc/qt-4.8/qprinter.html#newPage whenever you want to move on to the next PDF page).


===Using third-party libraries===
* '''''Scribe'''''<br />For a more high-level API for creating structured rich-text documents, use Qt's Scribe framework (see [[Handling_Document_Formats | Handling Document Formats]]). You can export the whole document or a part of it to PDF with &quot;QTextDocument::print&amp;quot;:/doc/qt-4.8/qtextdocument.html#print or &quot;QTextEdit::print&amp;quot;:/doc/qt-4.8/qtextedit.html#print (again, using a QPrinter object set up as shown above).


If you need more control over the output when <font color="#709">creating</font> <span class="caps">PDF</span> documents, or you need to <font color="#709">parse</font> existing <span class="caps">PDF</span> documents (anything from extracting specific information to assembling a full in-memory document object tree) and maybe even <font color="#709">modify</font> their structure or content before writing them back to disk, refer to third-party <span class="caps">PDF</span> reading/writing libraries:
* '''''Graphics View'''''<br />Qt's &quot;Graphics View framework&amp;quot;:/doc/qt-4.8/graphicsview.html can be a more suitable alternative for creating PDF documents with content that is mainly based on arbitrarily positioned and transformed 2D graphical items rather than continuous flowed rich text.<br />To export the content of a graphics scene or view (or a part of it) to PDF, you need to manually initialize a QPainter configured to paint on a PDF-creating QPrinter (as described above), and pass it to &quot;QGraphicsScene::render&amp;quot;:/doc/qt-4.8/qgraphicsscene.html#render or &quot;QGraphicsView::render&amp;quot;:/doc/qt-4.8/qgraphicsview.html#render.


{| class="infotable line" style="width: 95%; margin-left: 2.5%"
=== Using third-party libraries ===
|
! <span class="caps">API</span>
! <font color="#709">parsing</font>
! <font color="#709">modifying</font>
! <font color="#709">creating</font>
! platforms
! license
|-
|
[http://freedesktop.org/wiki/Software/poppler '''poppler-qt4'''] ''[freedesktop.org]''
| C++/Qt
| <font color="#580">yes</font>
| <font color="#920">?</font>
| <font color="#920">?</font>
| Win, Mac?, Linux, …
| <span class="caps">GPL</span> v2+ <font color="#458">[strong copyleft]</font>
|-
|
[http://pdfhummus.com/ '''Hummus'''] ''[pdfhummus.com]''
| C++
| <font color="#580">yes</font>
| <font color="#580">yes</font>
| <font color="#580">yes</font>
| Win, Mac, Linux
| Apache 2.0 <font color="#458">[permissive]</font>
|-
|
[http://podofo.sourceforge.net '''PoDoFo'''] ''[podofo.sourceforge.net]''
| C++
| <font color="#580">yes</font>
| <font color="#580">yes</font>
| <font color="#580">yes</font>
| Win, Mac, Linux
| <span class="caps">LGPL</span> <font color="#458">[weak copyleft]</font>
|}


===Using batch conversion tools===
If you need more control over the output when {color:#709}creating PDF documents, or you need to {color:#709}parse existing PDF documents (anything from extracting specific information to assembling a full in-memory document object tree) and maybe even {color:#709}modify their structure or content before writing them back to disk, refer to third-party PDF reading/writing libraries:


If all else fails, there is always the option of using an existing tool to automatically convert between <span class="caps">PDF</span> files and a more manageable format, and let your Qt application read/write that format instead. The conversion tool could be bundled with your application or specified as a prerequisite, and controlled via [[doc/QProcess|QProcess]]. Some possibilities are:
table{width:95%;margin-left:2.5%}.<br />| |''. API |''. {color:#709}parsing |''. {color:#709}modifying |''. {color:#709}creating |''. platforms |''. license |<br />| &quot;'''poppler-qt4'''&quot;:http://freedesktop.org/wiki/Software/poppler | C+''/Qt | {color:#580}yes | {color:#920}? | {color:#920}? | Win, Mac?, Linux, … | GPL v2'' {color:#458}[strong copyleft] |<br />| &quot;'''Hummus'''&quot;:http://pdfhummus.com/ | C++ | {color:#580}yes | {color:#580}yes | {color:#580}yes | Win, Mac, Linux | Apache 2.0 {color:#458}[permissive] |<br />| &quot;'''PoDoFo'''&quot;:http://podofo.sourceforge.net | C++ | {color:#580}yes | {color:#580}yes | {color:#580}yes | Win, Mac, Linux | LGPL {color:#458}[weak copyleft] |


{| class="infotable line" style="width: 95%; margin-left: 2.5%"
=== Using batch conversion tools ===
!
! executable names
! <font face="monospace">.pdf</font> to:
! … to <font face="monospace">.pdf</font>
! platforms
! license
|-
|
[http://freedesktop.org/wiki/Software/poppler '''poppler-utils'''] ''[freedesktop.org]''
| pdftotext, pdftocairo, pdftohtml
| <font face="monospace">.txt .svg .html …</font>
| <font face="monospace">–</font>
| Win, Mac?, Linux, …
| <span class="caps">GPL</span> v2+ <font color="#458">[strong copyleft]</font>
|-
|
[http://inkscape.org '''Inkscape'''] ''[inkscape.org]''
| inkscape
| <font face="monospace">.svg …</font>
| <font face="monospace">.svg …</font>
| Win, Mac, Linux, …
| <span class="caps">GPL</span> v2 <font color="#458">[strong copyleft]</font>
|}


==Rendering==
If all else fails, there is always the option of using an existing tool to automatically convert between PDF files and a more manageable format, and let your Qt application read/write that format instead. The conversion tool could be bundled with your application or specified as a prerequisite, and controlled via [[Doc:QProcess]]. Some possibilities are:


===Using third-party libraries/tools===
table{width:95%;margin-left:2.5%}.<br />|''. |''. executable names |''. {font-family:monospace}.pdf to: |''. … to {font-family:monospace}.pdf |''. platforms |''. license |<br />| &quot;'''poppler-utils'''&quot;:http://freedesktop.org/wiki/Software/poppler | pdftotext, pdftocairo, pdftohtml | {font-family:monospace}.txt .svg .html … | {font-family:monospace}<s>% | Win, Mac?, Linux, … | GPL v2+ {color:#458}[strong copyleft]|<br />| &quot;'''Inkscape'''&quot;:http://inkscape.org | inkscape | {font-family:monospace}.svg … | {font-family:monospace}.svg … | Win, Mac, Linux, … | GPL v2 {color:#458}[strong copyleft]|
<br />h2. Rendering
<br />h3. Using third-party libraries/tools
<br />For rendering pages or elements from existing PDF documents to image files or in-memory pixmaps (useful e.g. for thumbnail generation or implementing custom viewers), third-party libraries can be used:
<br />table{width:95%;margin-left:2.5%}.<br />| |''. API |''. can render |''. output to |''. platforms |''. license |<br />| &quot;'''poppler-qt4'''&quot;:http://freedesktop.org/wiki/Software/poppler | C+''/Qt | pages, …? | QImage | Win, Mac?, Linux, … | GPL v2'' {color:#458}[strong copyleft] |<br />| &quot;'''muPDF'''&quot;:http://mupdf.com | C | pages | RGBA byte array | Win, Mac, Linux, … | GPL v3+ {color:#458}[strong copyleft]; or commercial |
<br />Alternatively, the task can be delegated to existing command-line tools:
<br />table{width:95%;margin-left:2.5%}.<br />| |''. executable names |''. can render |''. output to |''. platforms |''. license |<br />| &quot;'''poppler-utils'''&quot;:http://freedesktop.org/wiki/Software/poppler | pdftocairo, pdftoppm, pdfimages | pages, image elements | {font-family:monospace}.png .jpg .svg .ppm … | Win, Mac?, Linux, … | GPL v2+ {color:#458}[strong copyleft]|<br />| &quot;'''muPDF'''&quot;:http://mupdf.com | pdfdraw | pages | {font-family:monospace}.png, .ppm, .pgm, .pam, .pbm | Win, Mac, Linux, … | GPL v3+ {color:#458}[strong copyleft]; or commercial |
<br />h2. Interactive Viewing
<br />h3. Calling an external viewer application
<br />If your application merely needs to let the user view/read certain PDF documents on demand, displaying them within the UI of the application itself might not be necessary, and delegating the task to an existing viewer application can be a viable option.
<br />Many users have already chosen and installed a stand-alone PDF viewer according to their personal preferences, so simply letting the operating system open the PDF file with whatever it considers the default viewer for such files, might be the easiest (and potentially most user-friendly) choice.<br />To do so, simply pass the PDF file's URL to &quot;QDesktopServices::openUrl&amp;quot;:/doc/qt-4.8/qdesktopservices.html#openUrl. If you're downloading the file from the Internet, store it on disk using [[Doc:QTemporaryFile]] first, since not all viewers can handle remote URLs.
<br />h3. Using a third-party Qt widget
<br />The following widgets provide native PDF viewing for Qt applications:
<br />table{width:95%;margin-left:2.5%}.<br />| |''. class name |''. platforms |''. license |<br />| &quot;'''XpdfWidget/Qt'''&quot;:http://www.glyphandcog.com/XpdfWidgetQt.html | XpdfWidget | Win, Mac, Linux, … | commercial |
<br />h3. Embedding a third-party ActiveX control
<br />If you are exclusively targeting the Windows platform, you can embed an existing ActiveX component for viewing PDFs in your Qt applications by instantiating it as a [[Doc:QAxWidget]] (see &quot;Qt's ActiveX Framework&amp;quot;:/doc/qt-4.8/activeqt.html).
<br />The following PDF viewers provide such an ActiveX control:
<br />table{width:95%;margin-left:2.5%}.<br />| |''. DLL file |''. ActiveX control name |''. platforms |''. license |<br />| &quot;'''Adobe Reader'''&quot;:http://get.adobe.com/reader/ | Acropdf.dll | {font-family:monospace}AxAcroPDFLib.AxAcroPDF | Win, Mac, Linux, … | {color:#458}freeware''(for commercial redistribution see &quot;here&amp;quot;:http://www.adobe.com/products/reader/distribution.html)_ |
<br />In the case of the Adobe Reader control, opening a PDF file is done with:<br /><code>dynamicCall(&quot;LoadFile&amp;amp;#40;const QString&amp;amp;#41;&quot;, pathToFile)<code>
<br />h3. Embedding a third-party browser plugin
<br />A more cross-platform technology for embedding reusable components is the &quot;NPAPI&amp;quot;:http://en.wikipedia.org/wiki/NPAPI browser plugin architecture</s> which Qt's WebKit-based browser framework &quot;happens to support&amp;quot;:/doc/qt-4.8/qtwebkit.html#netscape-plugin-support. You'll need to set up a simple HTML page containing appropriate{font-family:monospace}&amp;lt;embed&amp;amp;gt;…&amp;lt;/embed&amp;amp;gt;% tags, and let a [[Doc:QWebView]] display it (with &quot;QWebSettings::PluginsEnabled&amp;quot;:/doc/qt-4.8/qwebsettings.html#WebAttribute-enum set to true).


For rendering pages or elements from existing <span class="caps">PDF</span> documents to image files or in-memory pixmaps (useful e.g. for thumbnail generation or implementing custom viewers), third-party libraries can be used:
The following applications provide a reusable NPAPI plugin for viewing PDF:


{| class="infotable line" style="width: 95%; margin-left: 2.5%"
table{width:95%;margin-left:2.5%}.<br />| |''. plugin name |''. platforms |''. license |<br />| &quot;'''Adobe Reader'''&quot;:http://get.adobe.com/reader/ | nppdf | Win, Mac, (Linux)[1], … | {color:#458}freeware''(for commercial redistribution see &quot;here&amp;quot;:http://www.adobe.com/products/reader/distribution.html)_ |
|
! <span class="caps">API</span>
! can render
! output to
! platforms
! license
|-
|
[http://freedesktop.org/wiki/Software/poppler '''poppler-qt4'''] ''[freedesktop.org]''
| C++/Qt
| pages, …?
| QImage
| Win, Mac?, Linux, …
| <span class="caps">GPL</span> v2+ <font color="#458">[strong copyleft]</font>
|-
|
[http://mupdf.com '''muPDF'''] ''[mupdf.com]''
| C
| pages
| <span class="caps">RGBA</span> byte array
| Win, Mac, Linux, …
| <span class="caps">GPL</span> v3+ <font color="#458">[strong copyleft]</font>; or commercial
|}


Alternatively, the task can be delegated to existing command-line tools:
fn1{font-size:0.9em;line-height:150%;font-style:italic;margin-left:0.5em;margin-right:0.5em;color:#555}. While in theory it should work on all Desktop platforms, application developers have &quot;reported problems&amp;quot;:/forums/viewthread/14055 in trying to get it to work with Qt Webkit on Linux.


{| class="infotable line" style="width: 95%; margin-left: 2.5%"
As an alternative to using QWebView for running the plugin, it is possible to use a third-party solution that allows embedding NPAPI plugins in a Qt application without the overhead of a full web browser instance:
|
! executable names
! can render
! output to
! platforms
! license
|-
|
[http://freedesktop.org/wiki/Software/poppler '''poppler-utils'''] ''[freedesktop.org]''
| pdftocairo, pdftoppm, pdfimages
| pages, image elements
| <font face="monospace">.png .jpg .svg .ppm …</font>
| Win, Mac?, Linux, …
| <span class="caps">GPL</span> v2+ <font color="#458">[strong copyleft]</font>
|-
|
[http://mupdf.com '''muPDF'''] ''[mupdf.com]''
| pdfdraw
| pages
| <font face="monospace">.png, .ppm, .pgm, .pam, .pbm</font>
| Win, Mac, Linux, …
| <span class="caps">GPL</span> v3+ <font color="#458">[strong copyleft]</font>; or commercial
|}


==Interactive Viewing==
table{width:95%;margin-left:2.5%}.<br />| |''. component type |''. has special convenience API for |''. platforms |''. license |<br />| &quot;'''QtitanMultimedia'''&quot;:http://www.devmachines.com/products/qtitanmultimedia-overview.html | QWidget | Adobe Reader, … | Win, Linux | {color:#458}commercial |


===Calling an external viewer application===
=== Implementing a custom viewer ===


If your application merely needs to let the user view/read certain <span class="caps">PDF</span> documents on demand, displaying them within the UI of the application itself might not be necessary, and delegating the task to an existing viewer application can be a viable option.
p{border:dashed 1px #a94;background:#fbf3dd;color:#530;padding:2pt 4pt;margin-left:2pt;margin-right:2pt;font-size:0.9em;line-height:150%;font-style:italic}. TODO: Tips for implementing a custom interactive viewer, using Qt and the PDF parsing and rendering libraries mentioned above


Many users have already chosen and installed a stand-alone <span class="caps">PDF</span> viewer according to their personal preferences, so simply letting the operating system open the <span class="caps">PDF</span> file with whatever it considers the default viewer for such files, might be the easiest (and potentially most user-friendly) choice.<br /> To do so, simply pass the <span class="caps">PDF</span> file’s <span class="caps">URL</span> to [[doc/qt-4.8/qdesktopservices.html#openUrl|QDesktopServices::openUrl]]. If you’re downloading the file from the Internet, store it on disk using [[doc/QTemporaryFile|QTemporaryFile]] first, since not all viewers can handle remote <span class="caps">URL</span>s.
p{color:#fff;border-bottom:solid 1px #ccc}. .


===Using a third-party Qt widget===
== See Also ==


The following widgets provide native <span class="caps">PDF</span> viewing for Qt applications:
* [[Handling_Document_Formats | Handling Document Formats]]
 
** ''other &quot;text document&amp;quot; formats:''
{| class="infotable line" style="width: 95%; margin-left: 2.5%"
*** [[Handling_HTML | HTML]]
|
*** [[Handling_RTF | RTF]]
! class name
*** [[Handling_Microsoft_Word_(file_format) | Microsoft Word]]
! platforms
*** [[Handling_OpenDocument_Text | OpenDocument Text]]
! license
|-
|
[http://www.glyphandcog.com/XpdfWidgetQt.html '''XpdfWidget/Qt'''] ''[glyphandcog.com]''
| XpdfWidget
| Win, Mac, Linux, …
| commercial
|}
 
===Embedding a third-party ActiveX control===
 
If you are exclusively targeting the Windows platform, you can embed an existing ActiveX component for viewing <span class="caps">PDF</span>s in your Qt applications by instantiating it as a [[doc/QAxWidget|QAxWidget]] (see [[doc/qt-4.8/activeqt.html|Qt’s ActiveX Framework]]).
 
The following <span class="caps">PDF</span> viewers provide such an ActiveX control:
 
{| class="infotable line" style="width: 95%; margin-left: 2.5%"
|
! <span class="caps">DLL</span> file
! ActiveX control name
! platforms
! license
|-
|
[http://get.adobe.com/reader/ '''Adobe Reader'''] ''[get.adobe.com]''
| Acropdf.dll
| <font face="monospace">AxAcroPDFLib.AxAcroPDF</font>
| Win, Mac, Linux, …
| <font color="#458">freeware</font> ''(for commercial redistribution see [http://www.adobe.com/products/reader/distribution.html here] ''[adobe.com]'')''
|}
 
In the case of the Adobe Reader control, opening a <span class="caps">PDF</span> file is done with:<br />
 
===Embedding a third-party browser plugin===
 
A more cross-platform technology for embedding reusable components is the [http://en.wikipedia.org/wiki/NPAPI <span class="caps">NPAPI</span>] ''[en.wikipedia.org]'' browser plugin architecture – which Qt’s WebKit-based browser framework [[doc/qt-4.8/qtwebkit.html#netscape-plugin-support|happens to support]]. You’ll need to set up a simple <span class="caps">HTML</span> page containing appropriate <font face="monospace">&lt;embed&gt;…&lt;/embed&gt;</font> tags, and let a [[doc/QWebView|QWebView]] display it (with [[doc/qt-4.8/qwebsettings.html#WebAttribute-enum|QWebSettings::PluginsEnabled]] set to true).
 
The following applications provide a reusable <span class="caps">NPAPI</span> plugin for viewing <span class="caps">PDF</span>:
 
<font color="#555">''<font size="0.9em"><sup>1</sup> While in theory it should work on all Desktop platforms, application developers have [[forums/viewthread/14055|reported problems]] in trying to get it to work with Qt Webkit on Linux.</font>''</font>
 
As an alternative to using QWebView for running the plugin, it is possible to use a third-party solution that allows embedding <span class="caps">NPAPI</span> plugins in a Qt application without the overhead of a full web browser instance:
 
{| class="infotable line" style="width: 95%; margin-left: 2.5%"
|
! component type
! has special convenience <span class="caps">API</span> for
! platforms
! license
|-
|
[http://www.devmachines.com/products/qtitanmultimedia-overview.html '''QtitanMultimedia'''] ''[devmachines.com]''
| QWidget
| Adobe Reader, …
| Win, Linux
| <font color="#458">commercial</font>
|}
 
===Implementing a custom viewer===
 
''<font size="0.9em"><font color="#530"><span class="caps">TODO</span>: Tips for implementing a custom interactive viewer, using Qt and the <span class="caps">PDF</span> parsing and rendering libraries mentioned above</font></font>''
 
<font color="#fff">.</font>
 
==See Also==
 
* [[Handling Document Formats]]
** ''other “text document” formats:''
*** [[Handling HTML|<span class="caps">HTML</span>]]
*** [[Handling RTF|<span class="caps">RTF</span>]]
*** [[Handling Microsoft Word file format|Microsoft Word]]
*** [[Handling OpenDocument Text|OpenDocument Text]]
 
===Categories:===
 
* [[:Category:Developing with Qt|Developing_with_Qt]]

Revision as of 14:13, 23 February 2015


[toc align_right="yes&quot; depth="2&quot;]

Handling PDF

This page discusses various available options for working with "Portable Document Format &#40;PDF&amp;#41;":http://en.wikipedia.org/wiki/Portable_Document_Format documents in your Qt application. Please also read the general considerations outlined on the Handling Document Formats page.

p{width:60%;border:solid 1px #99a;background:#eef;color:#335;padding:2pt 4pt;font-size:0.9em;line-height:150%;font-style:italic}. Note that this information is collaboratively collected by the community, with no promise of completeness or correctness. In particular, use your own research and judgment when evaluating third-party libraries or tools!

Reading / Writing

Using QPrinter

For {color:#709}creating PDF documents from scratch, you can use Qt's built-in print support which also allows "printing&quot; to PDF files. To do so you can set up a Doc:QPrinter instance like this:

QPrinter printer(QPrinter::HighResolution);<br />printer.setOutputFormat(QPrinter::PdfFormat);<br />printer.setOutputFileName(&quot;path/to/file.pdf&amp;quot;);

Since QPrinter inherits Doc:QPaintDevice, anything that supports outputting graphical content to a QPaintDevice (or has convenience API for printing with QPrinter) can thus be used for generating PDFs:

  • manual QPainter painting
    The most basic (but not necessarily simplest) way of creating PDF documents with QPrinter is by manually painting the document's content with Qt's "Arthur paint system&quot;:/doc/qt-4.8/qt4-arthur.html.
    Just pass the QPrinter object as a reference to the constructor of Doc:QPainter (or, alternatively, to "QPainter::begin&quot;:/doc/qt-4.8/qpainter.html#begin for an already existing QPainter) and then perform any painting operations with that QPainter instance like you usually would (with intermittent calls to "QPrinter::newPage&quot;:/doc/qt-4.8/qprinter.html#newPage whenever you want to move on to the next PDF page).
  • Scribe
    For a more high-level API for creating structured rich-text documents, use Qt's Scribe framework (see Handling Document Formats). You can export the whole document or a part of it to PDF with "QTextDocument::print&quot;:/doc/qt-4.8/qtextdocument.html#print or "QTextEdit::print&quot;:/doc/qt-4.8/qtextedit.html#print (again, using a QPrinter object set up as shown above).
  • Graphics View
    Qt's "Graphics View framework&quot;:/doc/qt-4.8/graphicsview.html can be a more suitable alternative for creating PDF documents with content that is mainly based on arbitrarily positioned and transformed 2D graphical items rather than continuous flowed rich text.
    To export the content of a graphics scene or view (or a part of it) to PDF, you need to manually initialize a QPainter configured to paint on a PDF-creating QPrinter (as described above), and pass it to "QGraphicsScene::render&quot;:/doc/qt-4.8/qgraphicsscene.html#render or "QGraphicsView::render&quot;:/doc/qt-4.8/qgraphicsview.html#render.

Using third-party libraries

If you need more control over the output when {color:#709}creating PDF documents, or you need to {color:#709}parse existing PDF documents (anything from extracting specific information to assembling a full in-memory document object tree) and maybe even {color:#709}modify their structure or content before writing them back to disk, refer to third-party PDF reading/writing libraries:

table{width:95%;margin-left:2.5%}.
| |. API |. {color:#709}parsing |. {color:#709}modifying |. {color:#709}creating |. platforms |. license |
| "poppler-qt4":http://freedesktop.org/wiki/Software/poppler | C+/Qt | {color:#580}yes | {color:#920}? | {color:#920}? | Win, Mac?, Linux, … | GPL v2 {color:#458}[strong copyleft] |
| "Hummus":http://pdfhummus.com/ | C++ | {color:#580}yes | {color:#580}yes | {color:#580}yes | Win, Mac, Linux | Apache 2.0 {color:#458}[permissive] |
| "PoDoFo":http://podofo.sourceforge.net | C++ | {color:#580}yes | {color:#580}yes | {color:#580}yes | Win, Mac, Linux | LGPL {color:#458}[weak copyleft] |

Using batch conversion tools

If all else fails, there is always the option of using an existing tool to automatically convert between PDF files and a more manageable format, and let your Qt application read/write that format instead. The conversion tool could be bundled with your application or specified as a prerequisite, and controlled via Doc:QProcess. Some possibilities are:

table{width:95%;margin-left:2.5%}.
|. |. executable names |. {font-family:monospace}.pdf to: |. … to {font-family:monospace}.pdf |. platforms |. license |
| "poppler-utils":http://freedesktop.org/wiki/Software/poppler | pdftotext, pdftocairo, pdftohtml | {font-family:monospace}.txt .svg .html … | {font-family:monospace}% | Win, Mac?, Linux, … | GPL v2+ {color:#458}[strong copyleft]|
| "Inkscape":http://inkscape.org | inkscape | {font-family:monospace}.svg … | {font-family:monospace}.svg … | Win, Mac, Linux, … | GPL v2 {color:#458}[strong copyleft]|
h2. Rendering
h3. Using third-party libraries/tools
For rendering pages or elements from existing PDF documents to image files or in-memory pixmaps (useful e.g. for thumbnail generation or implementing custom viewers), third-party libraries can be used:
table{width:95%;margin-left:2.5%}.
| |. API |. can render |. output to |. platforms |. license |
| "poppler-qt4":http://freedesktop.org/wiki/Software/poppler | C+
/Qt | pages, …? | QImage | Win, Mac?, Linux, … | GPL v2 {color:#458}[strong copyleft] |
| "muPDF":http://mupdf.com | C | pages | RGBA byte array | Win, Mac, Linux, … | GPL v3+ {color:#458}[strong copyleft]; or commercial |

Alternatively, the task can be delegated to existing command-line tools:
table{width:95%;margin-left:2.5%}.
| |. executable names |. can render |. output to |. platforms |. license |
| "poppler-utils":http://freedesktop.org/wiki/Software/poppler | pdftocairo, pdftoppm, pdfimages | pages, image elements | {font-family:monospace}.png .jpg .svg .ppm … | Win, Mac?, Linux, … | GPL v2+ {color:#458}[strong copyleft]|
| "muPDF":http://mupdf.com | pdfdraw | pages | {font-family:monospace}.png, .ppm, .pgm, .pam, .pbm | Win, Mac, Linux, … | GPL v3+ {color:#458}[strong copyleft]; or commercial |

h2. Interactive Viewing
h3. Calling an external viewer application
If your application merely needs to let the user view/read certain PDF documents on demand, displaying them within the UI of the application itself might not be necessary, and delegating the task to an existing viewer application can be a viable option.
Many users have already chosen and installed a stand-alone PDF viewer according to their personal preferences, so simply letting the operating system open the PDF file with whatever it considers the default viewer for such files, might be the easiest (and potentially most user-friendly) choice.
To do so, simply pass the PDF file's URL to "QDesktopServices::openUrl&quot;:/doc/qt-4.8/qdesktopservices.html#openUrl. If you're downloading the file from the Internet, store it on disk using Doc:QTemporaryFile first, since not all viewers can handle remote URLs.
h3. Using a third-party Qt widget
The following widgets provide native PDF viewing for Qt applications:
table{width:95%;margin-left:2.5%}.
| |. class name |. platforms |. license |
| "XpdfWidget/Qt":http://www.glyphandcog.com/XpdfWidgetQt.html | XpdfWidget | Win, Mac, Linux, … | commercial |

h3. Embedding a third-party ActiveX control
If you are exclusively targeting the Windows platform, you can embed an existing ActiveX component for viewing PDFs in your Qt applications by instantiating it as a Doc:QAxWidget (see "Qt's ActiveX Framework&quot;:/doc/qt-4.8/activeqt.html).
The following PDF viewers provide such an ActiveX control:
table{width:95%;margin-left:2.5%}.
| |. DLL file |. ActiveX control name |. platforms |. license |
| "Adobe Reader":http://get.adobe.com/reader/ | Acropdf.dll | {font-family:monospace}AxAcroPDFLib.AxAcroPDF | Win, Mac, Linux, … | {color:#458}freeware(for commercial redistribution see "here&quot;:http://www.adobe.com/products/reader/distribution.html)_ |
In the case of the Adobe Reader control, opening a PDF file is done with:
dynamicCall("LoadFile&amp;#40;const QString&amp;#41;", pathToFile)
h3. Embedding a third-party browser plugin
A more cross-platform technology for embedding reusable components is the "NPAPI&quot;:http://en.wikipedia.org/wiki/NPAPI browser plugin architecture
which Qt's WebKit-based browser framework "happens to support&quot;:/doc/qt-4.8/qtwebkit.html#netscape-plugin-support. You'll need to set up a simple HTML page containing appropriate{font-family:monospace}&lt;embed&amp;gt;…&lt;/embed&amp;gt;% tags, and let a Doc:QWebView display it (with "QWebSettings::PluginsEnabled&quot;:/doc/qt-4.8/qwebsettings.html#WebAttribute-enum set to true).

The following applications provide a reusable NPAPI plugin for viewing PDF:

table{width:95%;margin-left:2.5%}.
| |. plugin name |. platforms |. license |
| "Adobe Reader":http://get.adobe.com/reader/ | nppdf | Win, Mac, (Linux)[1], … | {color:#458}freeware
(for commercial redistribution see "here&quot;:http://www.adobe.com/products/reader/distribution.html)_ |

fn1{font-size:0.9em;line-height:150%;font-style:italic;margin-left:0.5em;margin-right:0.5em;color:#555}. While in theory it should work on all Desktop platforms, application developers have "reported problems&quot;:/forums/viewthread/14055 in trying to get it to work with Qt Webkit on Linux.

As an alternative to using QWebView for running the plugin, it is possible to use a third-party solution that allows embedding NPAPI plugins in a Qt application without the overhead of a full web browser instance:

table{width:95%;margin-left:2.5%}.
| |. component type |. has special convenience API for |. platforms |. license |
| "QtitanMultimedia":http://www.devmachines.com/products/qtitanmultimedia-overview.html | QWidget | Adobe Reader, … | Win, Linux | {color:#458}commercial |

Implementing a custom viewer

p{border:dashed 1px #a94;background:#fbf3dd;color:#530;padding:2pt 4pt;margin-left:2pt;margin-right:2pt;font-size:0.9em;line-height:150%;font-style:italic}. TODO: Tips for implementing a custom interactive viewer, using Qt and the PDF parsing and rendering libraries mentioned above

p{color:#fff;border-bottom:solid 1px #ccc}. .

See Also