javascript - In-browser conversion of MS Word document to PDF - Stack Overflow

I would like to implement an in-browser Microsoft Word document merge feature that will convert the mer

I would like to implement an in-browser Microsoft Word document merge feature that will convert the merged document into PDF and offer it to the user for download. I would like to this process to be supported in Google Chrome and Firefox. Here is how I would like it to work:

  1. Client-side JavaScript obtains the Word template document in docx format, either from a server, or by asking the user for a file upload (which it can then read using the FileReader API)
  2. The JavaScript uses its local data structures (e.g., data lists it has obtained via Ajax) to expand the template into a document. It can do this either directly, by unzipping the docx file and processing its contents, or using DOCx.js. The template expansion is just a matter of substituting template variables with values obtained from the local data structures.
  3. The JavaScript then converts the expanded template into PDF.
  4. The JavaScript offers the PDF file to the user for download, e.g., using Downloadify.

The difficulty I am having is in step 3. My understanding (based on all the Googling I have done so far) is that I have the following options:

  1. Require that the local machine is a Windows machine, and invoke Word on it, to convert to PDF. This can be done using a little bit of scripting using WScript.shell, and it looks doable with Internet Explorer. But based on what I have read, it doesn't look like I can call WScript.shell from within either Chrome or Firefox, because of their security constraints.
  2. I am open to trying Silverlight to do the conversion, but I have not found enough documentation on how to do this. Ideally, if I used Silverlight, I would like to write the Silverlight code in JavaScript, because (a) I don't know much CSharp, and (b) I think it would be much easier in JavaScript.
  3. Create a web service that will convert a given docx file to a pdf file, and invoke that service via Ajax. I would rather not do this, if possible, for a few reasons: (a) I tried using docx4java (I am a reasonably skilled Java programmer) but the conversion process is far too slow, and it does not preserve document content very well; and (b) I would like to avoid a call out to the network, to avoid security issues. It does seem possible to write a little service on a Windows server for doing the conversion, and if there is no other good option, I might go that route.

If I have been unclear about anything, please let me know. I would appreciate your ideas and feedback.

I would like to implement an in-browser Microsoft Word document merge feature that will convert the merged document into PDF and offer it to the user for download. I would like to this process to be supported in Google Chrome and Firefox. Here is how I would like it to work:

  1. Client-side JavaScript obtains the Word template document in docx format, either from a server, or by asking the user for a file upload (which it can then read using the FileReader API)
  2. The JavaScript uses its local data structures (e.g., data lists it has obtained via Ajax) to expand the template into a document. It can do this either directly, by unzipping the docx file and processing its contents, or using DOCx.js. The template expansion is just a matter of substituting template variables with values obtained from the local data structures.
  3. The JavaScript then converts the expanded template into PDF.
  4. The JavaScript offers the PDF file to the user for download, e.g., using Downloadify.

The difficulty I am having is in step 3. My understanding (based on all the Googling I have done so far) is that I have the following options:

  1. Require that the local machine is a Windows machine, and invoke Word on it, to convert to PDF. This can be done using a little bit of scripting using WScript.shell, and it looks doable with Internet Explorer. But based on what I have read, it doesn't look like I can call WScript.shell from within either Chrome or Firefox, because of their security constraints.
  2. I am open to trying Silverlight to do the conversion, but I have not found enough documentation on how to do this. Ideally, if I used Silverlight, I would like to write the Silverlight code in JavaScript, because (a) I don't know much CSharp, and (b) I think it would be much easier in JavaScript.
  3. Create a web service that will convert a given docx file to a pdf file, and invoke that service via Ajax. I would rather not do this, if possible, for a few reasons: (a) I tried using docx4java (I am a reasonably skilled Java programmer) but the conversion process is far too slow, and it does not preserve document content very well; and (b) I would like to avoid a call out to the network, to avoid security issues. It does seem possible to write a little service on a Windows server for doing the conversion, and if there is no other good option, I might go that route.

If I have been unclear about anything, please let me know. I would appreciate your ideas and feedback.

Share Improve this question asked Nov 20, 2012 at 15:54 M. A. SridharM. A. Sridhar 812 silver badges4 bronze badges 4
  • 4 Why does it have to be client side? This would probably be much easier using a server side service (like a headless OpenOffice instance that does the conversion) – Pekka Commented Nov 20, 2012 at 15:56
  • 2 On the client side you have to build a plugin for every browser (and for every platform possibly), it will take you a lot of time. – Francesco Belladonna Commented Nov 20, 2012 at 15:58
  • Hey man, its not a good idea. if you need to do, just write an activex that just works on ie, and you need c++ experience on windows MFC classes.so painful. – pylover Commented Nov 20, 2012 at 16:02
  • Ever considerd SharePoint 2010 Word Automation Services? – Sheng Jiang 蒋晟 Commented Nov 21, 2012 at 0:01
Add a ment  | 

2 Answers 2

Reset to default 3

I love mand line tools.

Load the doc to your server and use LibreOffice to convert it to PDF via the mand line

soffice.exe --headless --convert-to pdf --outdir E:\Docs\Out E:\Docs\In\a.doc

You can display a progress bar to the user and when plete give them the option to download the doc.

More info on LibreOffice's mand line parameters go here

Done.

Old old question now, but for anyone who stumbles across this, web assembly (wasm) now makes this sort of approach possible.

We've just released https://www.npmjs./package/@nativedocuments/docx-wasm which can perform the conversion locally.

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744768875a4592632.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信