ASP.NET MVC:渲染视图以生成PDF:使用iTextSharp还是更好的解决方案?

时间:2021-06-29 21:10:39

I display receipt in both HTML and printer-friendly version. HTML version does jQuery tabs, etc, while printer-friendly has zero scripts and external dependencies, no master layout, no additional buttons, inline CSS, and can be saved as HTML without problems.

我在HTML和打印机友好版本中显示收据。 HTML版本执行jQuery选项卡等,而打印机友好的没有脚本和外部依赖,没有主布局,没有其他按钮,内联CSS,并且可以保存为HTML而没有问题。

Since I use Spark View Engine, I though maybe it's a good idea to generate PDF using iTextSharp engine. But after few paragraphs I decided it's too cumbersome, because a) I would have to rewrite entire receipt (source Spark view is about 5 pages long) b) I had problems with iTextSharp from beginning - for example, numbered lists kept bulleted, with no indentation, and indentationLeft="20" didn't work - maybe because of lack of documentation, but see (a).

由于我使用的是Spark View引擎,我认为使用iTextSharp引擎生成PDF是个好主意。但是在几段后我觉得它太麻烦了,因为a)我必须重写整个收据(源Spark视图大约5页长)b)我从头开始遇到iTextSharp的问题 - 例如,编号列表保持项目符号,没有缩进和indentationLeft =“20”不起作用 - 可能是因为缺少文档,但请看(a)。

So, my requirements for PDF are very simple: I want to keep the same HTML but insert page breaks between individual receipts (yes I have several ones in a single document).

因此,我对PDF的要求非常简单:我希望保留相同的HTML,但在单个收据之间插入分页符(是的,我在单个文档中有几个)。

Is there a simple way to generate PDF from view/HTML without rewriting the view using a strange half-documented engine?

是否有一种简单的方法可以从视图/ HTML生成PDF而无需使用奇怪的半文档引擎重写视图?

UPDATE: tried community HTMLDoc version; didn't use my inline CSS styles, incorrectly displayed Unicode symbols for currencies. wkhtmltopdf did pick the CSS but failed for currency symbol; I suppose there's problem with encoding solved by setting charset to utf-8. wkhtmltopdf seems to be nice but I'm yet to figure out how to set page breaks...

更新:尝试过社区HTMLDoc版本;没有使用我的内联CSS样式,错误地显示货币的Unicode符号。 wkhtmltopdf确实选择了CSS但货币符号失败了;我想通过将字符集设置为utf-8来解决编码问题。 wkhtmltopdf似乎很好,但我还没弄明白如何设置分页符...

3 个解决方案

#1


1  

If you can have the HTML in memory then you can convert it to PDF. I've once did something similar using xhtmlrenderer. It is a JAVA framework that bundles iText and that is capable of converting an HTML stream into PDF. As it is written in JAVA I've used the ikvmc.exe to convert the jar file into a .NET assembly and use it directly from managed code.

如果您可以在内存中使用HTML,则可以将其转换为PDF。我曾经使用xhtmlrenderer做过类似的事情。它是一个捆绑iText的JAVA框架,能够将HTML流转换为PDF。由于它是用JAVA编写的,我使用ikvmc.exe将jar文件转换为.NET程序集,并直接从托管代码中使用它。

#2


1  

 public class Pdf : IPdf
    {
        public FileStreamResult Make(string s)
        {
            using (var ms = new MemoryStream())
            {
                using (var document = new Document())
                {
                    PdfWriter.GetInstance(document, ms);
                    document.Open();
                    using (var str = new StringReader(s))
                    {

                        var htmlWorker = new HTMLWorker(document);

                        htmlWorker.Parse(str);
                    }
                    document.Close();
                }

                HttpContext.Current.Response.ContentType = "application/pdf";
                HttpContext.Current.Response.AddHeader("content-disposition", "attachment;filename=MyPdfName.pdf");
                HttpContext.Current.Response.Buffer = true;
                HttpContext.Current.Response.Clear();
                HttpContext.Current.Response.OutputStream.Write(ms.GetBuffer(), 0, ms.GetBuffer().Length);
                HttpContext.Current.Response.OutputStream.Flush();
                HttpContext.Current.Response.End();

                return new FileStreamResult(HttpContext.Current.Response.OutputStream, "application/pdf");
            }
        }
    }

#3


0  

Finally used wkhtmltopdf which works fine when I set encoding, I found out how to setup page breaks, and it processes my CSS very nice. On issue is that it can't correctly process stdin/out in Windows version (don't remember if it's in or out that doesn't work) - may be fixed in recent versions, but I'm ok with temp files.

最后使用wkhtmltopdf,当我设置编码时工作正常,我发现如何设置分页符,它处理我的CSS非常好。问题在于它无法在Windows版本中正确处理stdin / out(不记得它是否进入或不运行) - 可能在最新版本中修复,但我对临时文件没问题。

#1


1  

If you can have the HTML in memory then you can convert it to PDF. I've once did something similar using xhtmlrenderer. It is a JAVA framework that bundles iText and that is capable of converting an HTML stream into PDF. As it is written in JAVA I've used the ikvmc.exe to convert the jar file into a .NET assembly and use it directly from managed code.

如果您可以在内存中使用HTML,则可以将其转换为PDF。我曾经使用xhtmlrenderer做过类似的事情。它是一个捆绑iText的JAVA框架,能够将HTML流转换为PDF。由于它是用JAVA编写的,我使用ikvmc.exe将jar文件转换为.NET程序集,并直接从托管代码中使用它。

#2


1  

 public class Pdf : IPdf
    {
        public FileStreamResult Make(string s)
        {
            using (var ms = new MemoryStream())
            {
                using (var document = new Document())
                {
                    PdfWriter.GetInstance(document, ms);
                    document.Open();
                    using (var str = new StringReader(s))
                    {

                        var htmlWorker = new HTMLWorker(document);

                        htmlWorker.Parse(str);
                    }
                    document.Close();
                }

                HttpContext.Current.Response.ContentType = "application/pdf";
                HttpContext.Current.Response.AddHeader("content-disposition", "attachment;filename=MyPdfName.pdf");
                HttpContext.Current.Response.Buffer = true;
                HttpContext.Current.Response.Clear();
                HttpContext.Current.Response.OutputStream.Write(ms.GetBuffer(), 0, ms.GetBuffer().Length);
                HttpContext.Current.Response.OutputStream.Flush();
                HttpContext.Current.Response.End();

                return new FileStreamResult(HttpContext.Current.Response.OutputStream, "application/pdf");
            }
        }
    }

#3


0  

Finally used wkhtmltopdf which works fine when I set encoding, I found out how to setup page breaks, and it processes my CSS very nice. On issue is that it can't correctly process stdin/out in Windows version (don't remember if it's in or out that doesn't work) - may be fixed in recent versions, but I'm ok with temp files.

最后使用wkhtmltopdf,当我设置编码时工作正常,我发现如何设置分页符,它处理我的CSS非常好。问题在于它无法在Windows版本中正确处理stdin / out(不记得它是否进入或不运行) - 可能在最新版本中修复,但我对临时文件没问题。