【问题标题】:Generating PDF byte array生成 PDF 字节数组
【发布时间】:2014-01-30 22:44:54
【问题描述】:

我有一个像这样的静态方法,我正在使用 ITextSharp 生成 PDF..

public static byte[] createPDF(string htmlstr) {
            var  html = @"<?xml version=""1.0"" encoding=""UTF-8""?>
             <!DOCTYPE html 
                 PUBLIC ""-//W3C//DTD XHTML 1.0 Strict//EN""
                ""http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"">
             <html xmlns=""http://www.w3.org/1999/xhtml"" xml:lang=""en"" lang=""en"">
                <head>
                    <title>Minimal XHTML 1.0 Document with W3C DTD</title>
                </head>
              <body>
                " + htmlstr + "</body></html>";

            // step 1: creation of a document-object
            Document document = new Document(PageSize.A4, 30, 30, 30, 30);

            MemoryStream msOutput = new MemoryStream();

            // step 2:
            // we create a writer that listens to the document
            // and directs a XML-stream to a file
            PdfWriter.GetInstance(document, msOutput);

            // step 3: we create a worker parse the document
            HTMLWorker worker = new HTMLWorker(document);

            // step 4: we open document and start the worker on the document
            document.Open();
            worker.StartDocument();

            // step 5: parse the html into the document
            worker.Parse(new StringReader(html));

            // step 6: close the document and the worker
            worker.EndDocument();
            worker.Close();
            document.Close();

            byte[] buffer = new byte[msOutput.Length];

            using (MemoryStream ms = new MemoryStream())
            {
                int read;
                while ((read = msOutput.Read(buffer, 0, buffer.Length)) > 0)
                {
                    ms.Write(buffer, 0, read);
                }

                msOutput.Close();

                return ms.ToArray();
            }
    }

我在调试的时候,经过worker.Parse(new StringReader(html))之后,MemoryStream.Length就不行了。

我已经看到了一些使用FileStream 的示例,但我不想创建新文件。为什么代码会出错?

【问题讨论】:

    标签: .net pdf itextsharp filestream memorystream


    【解决方案1】:

    您遇到的基本问题是默认情况下PdfWriter 对象将关闭您正在写入的流。 PdfWriter.GetInstance() 实际上返回一个对象,您可以设置其他属性并且您特别想调用:

    writer.CloseStream = false;
    

    你的MemoryStreambyte[]MemoryStreambyte[] 让我很困惑,这只是解决上述问题的一种方法吗?

    从 iTextSharp 5.0.6 开始,大多数主要类,例如 DocumentPdfWriter 都实现了 IDisposable,至少对我而言,与 using 模式一起使代码更易于阅读和调试.您也不必考虑关闭事物。试一试:

    public static byte[] createPDF(string htmlstr) {
        var html = @"<?xml version=""1.0"" encoding=""UTF-8""?>
         <!DOCTYPE html 
             PUBLIC ""-//W3C//DTD XHTML 1.0 Strict//EN""
            ""http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"">
         <html xmlns=""http://www.w3.org/1999/xhtml"" xml:lang=""en"" lang=""en"">
            <head>
                <title>Minimal XHTML 1.0 Document with W3C DTD</title>
            </head>
          <body>
            " + htmlstr + "</body></html>";
    
    
        // step 1: creation of a document-object
        using (Document document = new Document(PageSize.A4, 30, 30, 30, 30)) {
            using (MemoryStream msOutput = new MemoryStream()) {
    
                // step 2:
                // we create a writer that listens to the document
                // and directs a XML-stream to a file
                using (PdfWriter writer = PdfWriter.GetInstance(document, msOutput)) {
    
                    // step 3: we create a worker parse the document
                    HTMLWorker worker = new HTMLWorker(document);
    
                    // step 4: we open document and start the worker on the document
                    document.Open();
                    worker.StartDocument();
    
    
                    // step 5: parse the html into the document
                    worker.Parse(new StringReader(html));
    
                    // step 6: close the document and the worker
                    worker.EndDocument();
                    worker.Close();
                    document.Close();
                }
    
                // Return the bytes
                return msOutput.ToArray();
            }
        }
    }
    

    【讨论】:

    • 谢谢克里斯。我看到我最初做错了什么,您的解决方案有所帮助。我想再补充一次我的问题。我遇到了 itextsharp 不支持 style="display:none" css 的问题,我想从中生成 PDF 的所有 HTML 都有。有没有办法在生成 PDF 时隐藏 HTML 中的一些元素?任何帮助将不胜感激。
    猜你喜欢
    • 2014-05-13
    • 1970-01-01
    • 1970-01-01
    • 2013-08-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-05-16
    • 1970-01-01
    相关资源
    最近更新 更多