How to use Docverter in AngularJS/NodeJS to convert GENERATED html to pdf for downloading

自闭症网瘾萝莉.ら 提交于 2019-12-24 15:33:41

问题


API of docverter is mentioned in curl format as shown below

curl \
  http://c.docverter.com/convert \
  -F from=html \
  -F to=pdf \
  -F input_files[]=@<(echo hello)

API states that input_files[] value should be a multipart/form-data file upload but in my AngularJS application, I am dynamically generating a report (at a specific route) which means it is not an html file which can be uploaded using file upload control.

My question may be bit vague because by looking at the docverter API, I am not able to know what code goes on client and what goes on server.

Overall I am looking for a solution which converts the generated HTML (along with stylesheet) to PDF file and this PDF file is then downloaded to the browser.

On the server side, I am using Node.js. Appreciate if you can provide clarity on how this conversion happens.


回答1:


Ok here is the server side fix to get the cors issues worked out with docverter.

It's actually very little code to get it up and running.

These changes happen in lib/docverter-server/app.rb

class DocverterServer::App < Sinatra::Base

# added code starts here ==>

before do
  headers 'Access-Control-Allow-Origin' => '*', 
          'Access-Control-Allow-Methods' => ['OPTIONS', 'GET', 'POST'] ,
          'Access-Control-Allow-Headers' => 'Content-Type',
          'Content-Disposition'  => 'attachment',
          'Content-Type' => 'application/octet-stream'
end

# <=== added ends starts here

set :show_exceptions, false

To fix errors so they don't just show cors conflicts

[500, {'Content-Type' => 'application/json', 'Access-Control-Allow-Origin' => '*'}, [MultiJson.dump(hash)]]

in lib/docverter-server/error-handler.rb

As for the angular client side => I document that fix with my own stack question here:

uploading text blob as file using $http or $resource with angular.js - mimicking curl style multipart/form upload



来源:https://stackoverflow.com/questions/33968401/how-to-use-docverter-in-angularjs-nodejs-to-convert-generated-html-to-pdf-for-do

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!