PDA

View Full Version : QPainter does not print html file as expected, ideas workarounds?



YaPeL
22nd October 2012, 23:39
Hi, I used the snipet provided by this (http://www.qtcentre.org/threads/64-how-to-print-an-html-file) thread to print a html file, however, I have a nasty html file, with tables, and styles inside the table, and stuff like that:



<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<HEAD>
<meta name="tipo_contenido" content="text/html;" http-equiv="content-type" charset="utf-8">
</HEAD>
<table border="1" cellspacing="0" cellpadding="0" width="100%" style="font-size:12px;line-height:20px;">
<tbody>
<tr>
<td valign="middle" align="left" width="40%" height="30">
Nombre:&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<br/>
Razon social:&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<br/>
</td>
<td valign="middle" align="left" width="20%" size="80">
<span style="font-size:8em;">&nbsp;</span>
</td>

<td valign="middle" align="left" width="40%" height="120">
Nro Comprobante: <br/>
Fecha y hora:<br/>
</td>

</tr>
<tr>
<td valign="TOP" colspan="2">
Domicilio comercial:<br/>
Domicilio Fiscal:<br/>
Iva: <br/>
</td>
<td valign="TOP" align="left" colspan="1">
Cuit: <br/>
Ing Brutos: <br/>
Inicio Actividades:<br/>
</td>
</tr>
<tr>
<td valign="middle" colspan="4" height="">
Identificacion:<br/>
Senior(es): <br/>
Domicilio: <br/>
Iva: <br/>
Cuit: <br/>
</td>
</tr>
</tbody>
</table>
<table border="1" RULES=COLS cellspacing="1" cellpadding="4" width="100%">
<tr>
<td width="96" align="right" height="22" align="middle" style="font-size:12px">NETO</td>
<td width="96" align="right" height="22" align="middle" style="font-size:12px">IVA FACT</td>
<td width="96" align="right" height="22" align="middle" style="font-size:12px">IVA SOB</td>
<td width="96" align="right" height="22" align="middle" style="font-size:12px">IVA 21%</td>
<td width="96" align="right" height="22" align="middle" style="font-size:12px">IVA 27%</td>
<td width="96" align="right" height="22" align="middle" style="font-size:12px">IVA 10.5%</td>
<td width="96" align="right" height="22" align="middle" style="font-size:12px">TOTAL</td>
</tr>
</table>
</html>


However Qt print it very different as how Chrome, firefox and IE do it... any idea or workaround? thanks!

wysota
23rd October 2012, 00:55
Define "very different"...

ChrisW67
23rd October 2012, 04:14
You are lucky it does anything at all. That "HTML" contains no <body></body> and is absolutely nothing like the XHTML it claims to be.

Have you tried feeding that mess through QtWebKit?

YaPeL
23rd October 2012, 04:25
No, I didn't try it, I was hoping that "setHtml" would have the same effect as feeding that html to a webKit Browser, but it didn't, chrome display that POS very nice, also, I have no clue on how to use QtWebKit to print an html file, any snipet? thanks

ChrisW67
23rd October 2012, 06:16
You couldn't search for "print" in the manual?


QPrinter *printer;
// set up printer

QWebView v;
v.setHtml(html);
v.show();
v.print(printer);

// or without the view widget
QWebPage page;
page.mainFrame()->setHtml(html);
page.mainFrame()->print(printer);


QTextDocument has a limited range of allowable HTML and does not cater for the huge range of malformed HTML in the wild. The browser components do, but they are much heavier components.

YaPeL
23rd October 2012, 17:02
You couldn't search for "print" in the manual?


QPrinter *printer;
// set up printer

QWebView v;
v.setHtml(html);
v.show();
v.print(printer);

// or without the view widget
QWebPage page;
page.mainFrame()->setHtml(html);
page.mainFrame()->print(printer);


QTextDocument has a limited range of allowable HTML and does not cater for the huge range of malformed HTML in the wild. The browser components do, but they are much heavier components.

Right, QWebPage could handle that monstruosity pretty well, thanks a lot!