wubzz / pdf-merge

Merge multiple PDF Files into a single PDF document
127 stars 32 forks source link

{files} parameter #7

Closed Ethaan closed 8 years ago

Ethaan commented 8 years ago

This is my use case.

I have an array of pdf URLs and im using fs.createWriteStream + http.get to generate files on the backend.

I get this working and im passing an array of objects to the PDFMerge like pdfMerge = new PDFMerge(pdfs)

But im getting an error of Object [object Object] has no method 'replace', here is the full error.

TypeError: Object [object Object] has no method 'replace' at /Users/Ethan-Mac/Documents/work/CRM/packages/npm-container/.npm/package/node_modules/shell-escape/shell-escape.js:9:17 at Array.forEach (native) at shellescape (/Users/Ethan-Mac/Documents/work/CRM/packages/npm-container/.npm/package/node_modules/shell-escape/shell-escape.js:7:5) at PDFMerge. (/Users/Ethan-Mac/Documents/work/CRM/packages/npm-container/.npm/package/nodemodules/pdf-merge/lib/PdfMerge.js:72:11) at Function..map._.collect (/Users/Ethan-Mac/Documents/work/CRM/packages/npm-container/.npm/package/node_modules/pdf-merge/nodemodules/underscore/underscore.js:172:24) at [object Object]..(anonymous function) [as map] (/Users/Ethan-Mac/Documents/work/CRM/packages/npm-container/.npm/package/node_modules/pdf-merge/node_modules/underscore/unders

Question, im doing this in the correct way?

wubzz commented 8 years ago

@Ethaan PDFMerge takes file paths, not file buffers. PDFtk is an external resource used to merge the pdf files, and thus cannot accept buffers/objects. One would have to store the files locally temporarily first. Either this can be done in your code, or you're free to make a pull request addressing this issue.

Ethaan commented 8 years ago

@wubzz right now im doing something like this.

  urlToFile: (files) ->
    check(files, Array)
    # array of files ready to be merged
    filesResponse = []
    # Keep track of each job in an array
    futures = _.map(files, (file) ->
      tempFile = null;
      # Set up a future for the current job
      future = new Future()
      # A callback so the job can signal completion
      onComplete = future.resolver()
      file_url = file.fileUrl
      options = {
        host: url.parse(file_url).host,
        port: 80,
        path: url.parse(file_url).pathname
      }
      file_name = url.parse(file_url).pathname.split('/').pop()
      tempFile = fs.createWriteStream(TEMP_FOLDER + file_name)
      http.get options, (res) ->
        res.on('data', (data) ->
          tempFile.write data
        ).on 'end', ->
          tempFile.end()
          console.log file_name + ' downloaded to ' + TEMP_FOLDER
          onComplete null, filesResponse.push tempFile.path
      future
      )
    Future.wait futures
    filesResponse

This code get the WEB URLs and create a write stream and store it on a temp folder.

Then i just call PDFMerge with the array of temp paths and its working cool now (or atleast its returning the buffer, i need to convert that buffer to a PDF).