html - font size and image size not respected when converting to pdf using itextsharp -
i've got code convert html pdf using itextsharp, however, when view pdf, font sizes , size of images different has been specified in html.
my code below - appreciated:
public function renderpdf() memorystream dim file new memorystream() dim document new itextsharp.text.document(me.pagesize) dim writer pdfwriter = pdfwriter.getinstance(document, file) document.open() dim tagprocessors defaulttagprocessorfactory = tags.gethtmltagprocessorfactory() tagprocessors.removeprocessor(html.tag.img) 'remove default processor tagprocessors.addprocessor(html.tag.img, new customimagetagprocessor()) 'use our new processor using document 'render each page has been added each page htmlpdfpage in me._pages document.newpage() 'generate page of text dim output new memorystream() dim html new streamwriter(output, encoding.utf8) dim sr new stringreader(page._html.tostring()) dim cssfiles new cssfilesimpl() cssfiles.add(xmlworkerhelper.getinstance().getdefaultcss()) dim cssresolver new styleattrcssresolver(cssfiles) dim charset = encoding.utf8 dim hpc new htmlpipelinecontext(new cssappliersimpl(new xmlworkerfontprovider())) hpc.setacceptunknown(true).autobookmark(true).settagfactory(tagprocessors) 'inject tagprocessors dim htmlpipeline new pipeline.html.htmlpipeline(hpc, new pdfwriterpipeline(document, writer)) dim pipeline new cssresolverpipeline(cssresolver, htmlpipeline) dim worker new xmlworker(pipeline, true) dim xmlparser new itextsharp.tool.xml.parser.xmlparser(true, worker, charset) xmlparser.parse(sr) next 'return rendered pdf return file end using end function public class customimagetagprocessor inherits itextsharp.tool.xml.html.image public overrides function [end](ctx iworkercontext, tag tag, currentcontent ilist(of ielement)) ilist(of ielement) dim attributes idictionary(of string, string) = tag.attributes dim src string if not attributes.trygetvalue(html.attribute.src, src) return new list(of ielement)(1) end if if string.isnullorempty(src) return new list(of ielement)(1) end if if src.startswith("data:image/", stringcomparison.invariantcultureignorecase) ' data:[<mime-type>][;charset=<encoding>][;base64],<data> dim base64data = src.substring(src.indexof(",") + 1) dim imagedata = convert.frombase64string(base64data) dim image = itextsharp.text.image.getinstance(imagedata) dim list = new list(of ielement)() dim htmlpipelinecontext = gethtmlpipelinecontext(ctx) list.add(getcssappliers().apply(new chunk(directcast(getcssappliers().apply(image, tag, htmlpipelinecontext), itextsharp.text.image), 0, 0, true), tag, htmlpipelinecontext)) return list else return mybase.[end](ctx, tag, currentcontent) end if end function end class
example html is:
<html> <head> <style id="mcedefaultstyles" type="text/css"> //<![cdata[ .mce-content-body div.mce-resizehandle {position: absolute;border: 1px solid black;background: #fff;width: 5px;height: 5px;z-index: 10000}.mce-content-body .mce-resizehandle:hover {background: #000}.mce-content-body img[data-mce-selected], hr[data-mce-selected] {outline: 1px solid black;resize: none}.mce-content-body .mce-clonedresizable {position: absolute;opacity: .5;filter: alpha(opacity=50);z-index: 10000} img:-moz-broken {-moz-force-broken-image-icon:1;min-width:24px;min-height:24px} //]]>// </style> <meta http-equiv="content-type" content="text/html; charset=utf-8" /> <link href="http://localhost:60699/scripts/tinymce/skins/lightgray/content.min.css" rel="stylesheet" type="text/css" /> </head> <body> <style id="mcedefaultstyles" type="text/css"> //<![cdata[ .mce-content-body div.mce-resizehandle {position: absolute;border: 1px solid black;background: #fff;width: 5px;height: 5px;z-index: 10000}.mce-content-body .mce-resizehandle:hover {background: #000}.mce-content-body img[data-mce-selected], hr[data-mce-selected] {outline: 1px solid black;resize: none}.mce-content-body .mce-clonedresizable {position: absolute;outline: 1px dashed black;opacity: .5;filter: alpha(opacity=50);z-index: 10000} //]]>// </style> <link href="http://localhost:59372/call_scripter/scripts/tinymce/skins/lightgray/content.min.css" rel="stylesheet" type="text/css" data-mce-href="http://localhost:59372/call_scripter/scripts/tinymce/skins/lightgray/content.min.css" /> <p> <br /> </p> <table width="1000" align="center" class="" cellspacing="0" cellpadding="0"> <tbody> <tr> <td valign="top" style="width: 560px; padding-left: 80px;" data-mce-style="width: 560px; padding-left: 80px;"> <strong><span style="font-size: 18px;" data-mce-style="font-size: 18px;">company name</span><br /> abn: 65 999 999 999<br /> po box 999<br /> suburb, state, postcode australia<br /> phone: 02 9999 9999 fax: 02 9999 9999<br /> email: anemailaddress@blahblah.com.au<br /> web: www.blahblah.com.au </strong> </td> <td valign="top" rowspan="2"> <strong><span style="font-size: 18px;" data-mce-style="font-size: 18px;">tax invoice</span> </strong> <table class="" data-mce-selected="1"> <tbody> <tr> <td width="170" style="font-weight: bold;" data-mce-style="font-weight: bold;"> invoice number: </td> <td style="font-weight: bold;" data-mce-style="font-weight: bold;"> </td> </tr> <tr> <td> </td> <td> </td> </tr> <tr> <td> sales order: </td> <td> </td> </tr> <tr> <td> invoice date: </td> <td> </td> </tr> <tr> <td> abn: </td> <td> 65 090 839 197 </td> </tr> <tr> <td> reference: </td> <td> </td> </tr> <tr> <td> <strong>page 1 of 1</strong> </td> <td> </td> </tr> </tbody> </table> </td> </tr> <tr> <td valign="bottom" style="padding-left: 160px;" data-mce-style="padding-left: 160px;"> <strong></strong>australia </td> </tr> <tr> <td style="padding-right: 25px;" colspan="2" data-mce-style="padding-right: 25px;"> <hr style="background: rgb(0, 0, 0); width: 100%; height: 2px; color: rgb(0, 0, 0); border-top-color: rgb(0, 0, 0); border-top-width: 1px; border-top-style: solid; display: block;" data-mce-style="display: block; width: 100%; height: 2px; background: transparent; background-color: #000; border-top: solid 1px #000; color: #000;" /> </td> </tr> <tr> <td align="right" valign="top" style="width: 560px; height: 900px; padding-left: 80px;" colspan="2" data-mce-style="width: 560px; padding-left: 80px; height: 900px;"> <table class="" style="width: 920px;" data-mce-selected="1" data-mce-style="width: 920px;"> <tbody> <tr> <td> <strong>product</strong> </td> <td> <strong>item description</strong> </td> <td align="center"> <strong>qty</strong> </td> <td align="right"> <strong>unit price (ex gst)</strong> </td> <td style="padding-left: 10px;" data-mce-style="padding-left: 10px;"> <strong>unit</strong> </td> <td align="right"> <strong>discount</strong> </td> <td align="right"> <strong>net amount</strong> </td> <td align="right"> <strong>gst</strong> </td> <td align="right"> <strong>total</strong> </td> </tr> <tr> <td> </td> <td style="width: 220px;" data-mce-style="width: 220px;"> </td> <td align="center"> 1 </td> <td align="right"> </td> <td> </td> <td> </td> <td align="right" style="width: 100px;" data-mce-style="width: 100px;"> </td> <td align="right" style="width: 100px;" data-mce-style="width: 100px;"> </td> <td align="right" style="width: 100px;" data-mce-style="width: 100px;"> </td> </tr> </tbody> </table> </td> </tr> <tr> <td align="right" style="font-weight: bold;" colspan="2" data-mce-style="font-weight: bold;"> <table align="right" class="" style="width: 450px;" data-mce-selected="1" data-mce-style="width: 450px;"> <tbody> <tr> <td> total page 1 </td> <td align="right" style="width: 100px;" data-mce-style="width: 100px;"> </td> <td align="right" style="width: 100px;" data-mce-style="width: 100px;"> </td> <td align="right" style="width: 100px;" data-mce-style="width: 100px;"> </td> </tr> <tr> <td> grand total aud </td> <td align="right" style="width: 100px;" data-mce-style="width: 100px;"> </td> <td align="right" style="width: 100px;" data-mce-style="width: 100px;"> </td> <td align="right" style="width: 100px;" data-mce-style="width: 100px;"> </td> </tr> <tr> <td> </td> <td align="center" style="text-align: left;" colspan="2" data-mce-style="text-align: left;"> balance owing </td> <td align="right" style="width: 100px;" data-mce-style="width: 100px;"> $0.00 </td> </tr> <tr> <td> </td> <td align="center" style="text-align: left;" colspan="2" data-mce-style="text-align: left;"> paid in full </td> <td align="right" style="width: 100px;" data-mce-style="width: 100px;"> </td> </tr> </tbody> </table> </td> </tr> <tr> <td style="padding-right: 25px;" colspan="2" data-mce-style="padding-right: 25px;"> <hr style="background: rgb(0, 0, 0); width: 100%; height: 2px; color: rgb(0, 0, 0); border-top-color: rgb(0, 0, 0); border-top-width: 1px; border-top-style: solid; display: block;" data-mce-style="display: block; width: 100%; height: 2px; background: transparent; background-color: #000; border-top: solid 1px #000; color: #000;" /> </td> </tr> </tbody> </table> <p> </p> </body> </html>
i ended ditching itextsharp html pdf conversion, , instead, went solution https://github.com/gmanny/pechkin - means can generate pdf 1 line of code , pdf comes out !
Comments
Post a Comment