如何在AngularJS / NodeJS中使用Docverter将GENERATED html转换为pdf以供下载

时间:2015-11-28 06:24:57

标签: angularjs node.js pdf docverter

docverter的API以curl格式提及,如下所示

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

API声明input_files[]值应该是multipart/form-data file上传,但在我的AngularJS应用程序中,我动态生成报告(在特定路线),这意味着它不是一个html文件,可以是使用文件上传控件上传。

我的问题可能有点模糊,因为通过查看docverter API,我无法知道客户端上的代码以及服务器上的代码。

总的来说,我正在寻找一种解决方案,将生成的HTML(以及样式表)转换为PDF文件,然后将此PDF文件下载到浏览器中。

在服务器端,我使用的是Node.js.感谢您是否可以清楚地说明此转换的发生方式。

1 个答案:

答案 0 :(得分:0)

好的,这是服务器端修复程序,以解决与docverter一起解决的问题。

实际上很少有代码可以启动并运行。

这些变化发生在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

修复错误,以便他们不仅仅显示角色冲突

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

lib/docverter-server/error-handler.rb

中的

角度客户端=&gt;我在这里记录了我自己的堆栈问题:

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