使用itextsharp转换为pdf时,不遵守字体大小和图像大小

时间:2014-03-19 23:04:11

标签: html css image pdf itextsharp

我有一些代码可以使用iTextSharp将HTML转换为PDF,但是,当我查看PDF时,图像的字体大小和大小与HTML中指定的不同。

我的代码如下 - 任何帮助将不胜感激:

Public Function RenderPdf() As MemoryStream

    Dim file As New MemoryStream()
    Dim document As New iTextSharp.text.Document(Me.PageSize)
    Dim writer As PdfWriter = PdfWriter.GetInstance(document, file)
    document.Open()

    Dim tagProcessors As DefaultTagProcessorFactory = Tags.GetHtmlTagProcessorFactory()
    tagProcessors.RemoveProcessor(HTML.Tag.IMG) 'remove the default processor
    tagProcessors.AddProcessor(HTML.Tag.IMG, New CustomImageTagProcessor()) 'use our new processor


    Using document
        'render each page that has been added
        For Each page As HtmlPdfPage In Me._Pages
            document.NewPage()

            'generate this page of text
            Dim output As New MemoryStream()
            Dim html As New StreamWriter(output, Encoding.UTF8)

            Dim sr As New StringReader(page._Html.ToString())

            Dim cssFiles As New CssFilesImpl()
            cssFiles.Add(XMLWorkerHelper.GetInstance().GetDefaultCSS())
            Dim cssResolver As New StyleAttrCSSResolver(cssFiles)
            Dim charset = Encoding.UTF8

            Dim hpc As New HtmlPipelineContext(New CssAppliersImpl(New XMLWorkerFontProvider()))
            hpc.SetAcceptUnknown(True).AutoBookmark(True).SetTagFactory(tagProcessors) 'inject the tagProcessors
            Dim htmlPipeline As New pipeline.html.HtmlPipeline(hpc, New PdfWriterPipeline(document, writer))
            Dim pipeline As New CssResolverPipeline(cssResolver, htmlPipeline)
            Dim worker As New XMLWorker(pipeline, True)
            Dim xmlParser As New iTextSharp.tool.xml.parser.XMLParser(True, worker, charset)
            xmlParser.Parse(sr)

        Next

        'return the rendered PDF
        Return file
    End Using

End Function


Public Class CustomImageTagProcessor
        Inherits iTextSharp.tool.xml.html.Image
        Public Overrides Function [End](ctx As IWorkerContext, tag As Tag, currentContent As IList(Of IElement)) As IList(Of IElement)
            Dim attributes As IDictionary(Of String, String) = tag.Attributes
            Dim src As String
            If Not attributes.TryGetValue(HTML.Attribute.SRC, src) Then
                Return New List(Of IElement)(1)
            End If

            If String.IsNullOrEmpty(src) Then
                Return New List(Of IElement)(1)
            End If

            If src.StartsWith("data:image/", StringComparison.InvariantCultureIgnoreCase) Then
                ' 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

示例HTML是:

<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">
                    &nbsp;<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>
                                    Your 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>

1 个答案:

答案 0 :(得分:0)

我最终放弃了iTextSharp进行HTML到PDF的转换,相反,我选择了这个解决方案https://github.com/gmanny/Pechkin - 这意味着我可以使用1行代码生成PDF并且PDF完美无缺!