[QT 5.9.1] Does QPrinter really work to create PDF documents?



  • Hi,
    I have an HTML document to convert to PDF. For this I'm reading it with a QWebEnginePage. In the output PDF file I must set at list the "Creator" metadata to a private value. Infortunately this does not seems to be possible if I use QWebEnginePage::printToPdf. So, I tried to use the common method, with a QPrinter. This is my code:

     QPrinter printer(QPrinter::HighResolution);
        printer.setOutputFormat(QPrinter::PdfFormat);
        printer.setPaperSize(QPrinter::A4);
        printer.setPageMargins(0.,0.,0.,0., QPrinter::Millimeter);
        printer.setFullPage(false);
        printer.setCreator(p_creator);
        printer.setOutputFileName(p_filename);
    
        // "Print" the file
        bool result = true;
    	QEventLoop blockingLoop;
    	m_webPage->print(&printer, [&blockingLoop](bool p_ok) 
    	{ 
    		std::cout << "OK: " << p_ok << std::endl; 
    		blockingLoop.exit(p_ok); 
    	});
    	result = blockingLoop.exec();
    
    

    Unfortunately this approach does not work because:

    • My text is in Calibri. And it seems to have a bug for this font in the conversion from glyph to unicode. In debug I have an abort because funicode is 0x424242 for one glyph ID. It seems that it is also this problem that generates a crash in debug when I use printToPdf. In release everything works well for printToPdf but I have an empty page if I use print with the QPrinter,

    • If I move to Arial I do not have the problem. But in this case the QPrinter (still in debug) abort because it is not able to generate the result QImage (of size 19834*28066). And again I have an empty page as result in release.

    I cannot replace the QWebEnginePage by a QTextDocument. Because QTextDocument does not take care of the CSS that is in the HTML file.

    So, do you know how to convert an HTML document in PDF, using Qt? Or, where is my error in my code?

    Thanks.

    Below the HTML file I try to read (cannot upload it because of missing rights). With or without the right links to images changes nothing.

    <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
    <html>
    <head>
        <title></title>
    	<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
    	<!-- ============================================== CSS Mandatory part ============================================= -->
    	<style type="text/css">
    	*
    	{
    		font-family: Arial;
    		font-size: 12pt;
    	}
    	
    	hr
    	{
    		background-color: #d3e9b3;
    		height: 1pt;
    		border:none;
    		margin:0;
    	}
    	
    	body
    	{
    		padding: 0cm;
    		margin:0cm;
    		border-width:0cm;
    	}
    	div.page
    	{
    		min-width: 21cm;
    		max-width: 21cm;
    		min-height:30.6cm;
    		max-height:30.6cm;
    		margin:0cm 0cm;
    		padding:1cm;
    		border-style:solid;
    		border-width:1mm;
    		border-color:#FFF;
    		position: relative;
    	}
    	div.footer
    	{
    		 position: absolute; 
    		 bottom: 0;
    		 vertical-align:bottom;
    	}
        </style>
    
     	<!-- ============================================== CSS Template specific part ============================================= -->
    	<style type="text/css">
    	span.adress
    	{
    		border-style:solid;
    		border-width:1pt;
    		border-color:#87c913;
    		float: left;
    		max-width: 6cm;
    		min-width: 6cm;
    		min-height:3cm;
    		padding: 0.2cm 0.4cm;
    		border-radius: 0.1cm;
    		margin: 0cm 0.2cm 0.2cm 0cm;
    	}
    	
    	span.name
    	{
    		min-height:1cm;
    		min-width:12cm;
    		font-weight:bold;
    		font-size:16pt;
    		float: left;
    		padding: 0cm 0.4cm;	
    		margin: 1cm 0cm 0cm 0cm;
    	}
    
    	td.header
    	{
    		padding:0cm;
    		background-color:#FFF;
    	}
        </style>
        
    </head>
    <!-- ============================================== head / body ============================================== -->
    <body>
        <!-- ================================= FIRST PAGE ======================================================== -->
    	<div class="page">
            <!--============== Top: Surgeon name + hopital adress + OTX logo ==============-->
    		<div style="margin:0;">
    			<table>
    				<tr>
    					<td class="header" colspan="2">
    						<span class="name">John W. Doe</span><br>
    					</td>
    					<td  class="header" style="text-align:right;" rowspan="2">
    						<IMG src="file:///C:/Dev/Tests/ReportManager/Resources/logo.png" alt="Il manque le logo !" height=200 />
    					</td>
    				</tr>
    				<tr>
    					<td class="header" style="width:3cm">
    						<span class="adress">Ici<br/>La</span>
    					</td>
     				</tr>
    			</table>
    		</div>
            <!--============== Header: Patient information ==============-->
    		<div style="margin:0; background-color: #808080;">
                <table>
                    <tr><td>Nom :</td><td></td></tr>
                    <tr><td>Date de naissance :</td><td></td></tr>
                    <tr><td>Dossier N&deg; :</td><td>172009-0000000666</td></tr>
                </table>
            </div>
            
            <!--============== Images =================================-->
     		<div style="margin:0;">
    			<table>
                    <tr>
                        <td width=800 align="center">
                            <table>
                                <tr><th>TITRE 1</th><th>TITRE 2</th></tr>
                                <tr><td>Texte 1</td><td>0Texte 2</td></tr>
                                <tr><td><IMG src="172009-0000000666_files/img1.png" alt="Il manque l'image img1" height=200 /></td><td><IMG src="172009-0000000666_files/img2.png" alt="Il manque l'image img2" height=200 /></td></tr>
                           </table>
                            <table>
                                <tr><th>TITRE 3</th><th>TITRE 4</th></tr>
                                <tr><td>Texte 3</td><td>Texte 4</td></tr>
                                <tr><td colspan=2><IMG src="172009-0000000666_files/img3" alt="Il manque l'image img3" width=200 /></td></tr>
                           </table>
                        </td>
                        <td width=800  align="center">
                            <IMG src="172009-0000000666_files/img4.png" alt="Il manque l'image img4"/>
                        </td>
                    </tr>
                </table>
            </div>
        </div>
    </body>
    
    


  • @Alain38 said in [QT 5.9.1] Does QPrinter really work to create PDF documents?:

    I have an HTML document to convert to PDF. For this I'm reading it with a QWebEnginePage. In the output PDF file I must set at list the "Creator" metadata to a private value. Infortunately this does not seems to be possible if I use QWebEnginePage::printToPdf.

    I use printToPdf() to export to PDF file; I don't use QPrinter. Your code implies to me you are directing to file rather than to actual printer too.

    If you are saying your code would work if you could use printToPdf(), but you need to set "Creator" metadata, as a workaround have you considered simply updating the resulting PDF file to what you desire after printToPdf() has completed? PDF file is simply text you can read/write (have a look at it), so inserting/replacing "Creator" is not too arduous. There is an extra time involved in having to read in & write out a whole PDF file which might be large, but insignificant compared to, say, the time to actually print or probably the time to generate the PDF originally....



  • @JNBarchan the option to "hack" the PDF file is not acceptable in my case to change the creator of the file. It is why I tried to use QPrinter. With QPrinter I'm normally able to fill the creator metadata. It worked with Qt 4.8.4. With 5.9 the option is still present. But this is the whole QPrinter process that no more work.



  • Is there a special reason you are not using QPdfWriter?



  • @Asperamanca How do you feed the QPdfWriter an HTML document to convert to PDF?


  • Moderators

    @JNBarchan You can paint a webview for example on a QPdfWriter.



  • @jsulm

    @JNBarchan You can paint a webview for example on a QPdfWriter.

    OK, so you can attach a QPdfWriter to a QWebView (somehow).

    For my purposes, I was interested to know whether Qt can generate PDF (to file) from HTML without going through a "visual"/"interactive" stage? But it seems to me the PDF generation is a feature of QWebView or QWebEnginePage widgets, and so inherently connected to visualization/interactivity, is that correct?


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.