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

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

API of docverter 提到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。如果您能清楚说明这种转换是如何发生的,我们将不胜感激。

好的,这里是服务器端修复程序,用于解决 docverter 的 cors 问题。

其实代码很少就可以搞定运行。

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

修复错误,使它们不只是显示 cors 冲突

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

lib/docverter-server/error-handler.rb

至于 angular 客户端 => 我在此处记录了我自己的堆栈问题修复:

使用 $http 或 $resource 和 angular.js 将文本 blob 作为文件上传 - 模仿 curl 样式 multipart/form 上传