Search code examples
angularjsnode.jspdfdocverter

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


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.


Solution

  • 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