开发者

ASP.NET website, server-side DOCX to PDF conversion

开发者 https://www.devze.com 2022-12-13 18:47 出处:网络
I\'ve been having a heckuva time with this problem, and there seems to be a lot of noise out there in search engines in getting to the bottom of it, so forgive me if I\'ve missed a silver bullet out t

I've been having a heckuva time with this problem, and there seems to be a lot of noise out there in search engines in getting to the bottom of it, so forgive me if I've missed a silver bullet out there.

The base need is that I have to generate a PDF document that has both static and dynamic elements. I started to do this by having a PDF template with all the static content, and then I wanted to inject various dynamic elements into it. The problem is that PDFs are not meant to be manipulated that way, and depending on the size of the dynamic text I put in there, might overflow text on other pages. I was using iTextSharp but can't get past this problem.

A possible fallback is to generate a DOCX, which I've done before, and then convert it into a PDF on the backend. The only libraries I've found to do this are paid apps (like Aspose). There are examples out there that convert to PDF without these libraries, but they seem to require a client-side application. I'm doing this via IIS.

To make a long story longer...are there free libr开发者_开发问答aries that will convert a DOCX file to a PDF server-side without launching client applications to do so?


There are a few choices here:

  • build a COM interop class that will perform read and 'Save As' functions on your .docx. The MSDN link you gave doesn't require to be run client-side, but rather have the Office assemblies in the GAC or in your ASP.NET's bin directory.

  • buy a third party component to do the work for you. Here's just one example with no guarantees.


I'm not familiar with any good free ones, but we used Aspose.Words to achieve something similar to what you describe. We keep Word templates with static text and mail-merge fields. The templates can be regular Word documents, they don't have to be .dot templates. Mail-merge fields can be either single fields or repeatable data in tables so you can easily generate pretty complex documents without doing dynamic document editing. (Which is always an option)

Using Aspose for this was so friction free that I would suggest using Aspose unless the cost (which is significant) is a show-stopper. The support is also good which is always an added bonus.

There are always some caveats...

  • I would have liked more control over the PDF compatability of the generated PDFs. We had some issues with older clients reading the generated PDFs.
  • Mail-merge is not fun. Complex mail-merge expressions was time consuming to get right.


I just found very simple solution to convert any files from command-line using LibreOffice:

soffice.exe --headless --convert-to pdf file.xls

(google for the rest)

0

精彩评论

暂无评论...
验证码 换一张
取 消