生成的PDF忽略了一些CSS& HTML

时间:2016-04-22 07:22:15

标签: c# html css pdf itextsharp

我的网络应用程序显示的div包括QR图像和数据库中的一些文本。通过iTextSharp,我将这个html页面生成为PDF。这很好用,问题是PDF没有读取一些CSS。我已经尝试了一切可能的方法来使用CSS(一些CSS和外部文件)

它在浏览器中看起来不错,但是当它生成为PDF时,唯一的相似之处是背景颜色。没有其他的。所以我需要知道是否有任何可能的方法来改变立场。 This is how it looks on the web ||| And this is how it looks like in the PDF 正如你所看到的那样,存在巨大的差异。该文本甚至没有在PDF中显示。我可以改变图像的高度和宽度,但不是裕度。

这是创建我的PDF的逻辑。

Byte[] bytes;  // This byte array will hold the final PDF.
        var cssText = File.ReadAllText(@"C:\Users\My_Name\documents\visual studio 2012\Projects\QR\QR\Content\CSS.css");

        var htmlText = File.ReadAllText(@"C:\Users\My_Name\documents\visual studio 2012\Projects\QR\QR\WebForm1.aspx");
        //Creating a stream that we can write to, - a memorystream in this case.
        using (var memoryStream = new MemoryStream())
        {
            //iTextSharp Document which is an abstraction of a PDF but NOT a PDF. 
            using (var doc = new Document())
            {
                using (var writer = PdfWriter.GetInstance(doc, memoryStream))
                {
                    doc.Open();
                    using (var memoryStreamCSS = new MemoryStream(System.Text.Encoding.UTF8.GetBytes(cssText)))
                    {
                        using (var memoryStreamHTML = new MemoryStream(System.Text.Encoding.UTF8.GetBytes(htmlText)))
                        {
                           XMLWorkerHelper.GetInstance().ParseXHtml(writer, doc, memoryStreamHTML, memoryStreamCSS);
                        }
                    }
                    doc.Close();
                }
            }
            bytes = memoryStream.ToArray();
        }
        var testFile = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Desktop), "Testing.pdf");
        File.WriteAllBytes(testFile, bytes);
    }

这是html:

 <form id="form1" runat="server">
    <asp:Panel ID="Panel1" runat="server">

        <div id="qr">
            <div class="qrImage">
                <img src="http://localhost:19859/Image/QrCode.jpg" id="qrImage"  />
            </div>
            <div class="information">
                <h1>
                    <asp:Label runat="server" ID="building"></asp:Label></h1>
                <p class="productName prodInfo">
                    <asp:Label runat="server" ID="productName"></asp:Label>
                </p>
                <p class="prodInfo">
                    <asp:Label runat="server" ID="productInfo"></asp:Label>
                </p>
                 <p class="prodInfo">
                    <asp:Label runat="server" ID="productSomething"></asp:Label>
                </p>
                 <p class="prodInfo">
                    <asp:Label runat="server" ID="productPlace"></asp:Label>
                </p>
            </div>

        </div>
    </asp:Panel>
    <asp:Button ID="btnExport" runat="server" Text="Export"
        OnClick="btnExport_Click" />


</form>

以下是生成QR码的代码:

   QRCodeEncoder encoder = new QRCodeEncoder();
            Bitmap img = encoder.Encode(_qrLink);  //Choose one special link to generate a QR-code from.
            img.Save(@"C:\Users\My_Name\Documents\visual studio 2012\Projects\QR\QR\Image\QrCode.jpg", ImageFormat.Jpeg);

这是我的CSS

body { font-family: arial;
width: 110mm;
height: 55mm;
}

table {
background-color: #e5daa2;
width: 100mm;
height: 50mm;
}


#qr {
background-color: #e5daa2;
width: 100mm;
height: 50mm;
margin-left: 10px;
-ms-border-radius: 10px;
border-radius: 10px;
}

.qrImage img {
margin-top: 10mm;
}
img {
width: 130px;
margin-left: 3mm;
margin-top: 10mm;
}

.information {
float: right;
margin-top: -38mm;
}

.prodInfo {
font-size: 11px;
 margin-top: -32mm;
margin-left: -60mm;
}

.productName {
font-weight: bold;
 font-size: 12px;
}

0 个答案:

没有答案