pofider / phantom-html-to-pdf

Highly scalable html to pdf conversion using phantom workers
MIT License
159 stars 33 forks source link

Clone the options var. #36

Closed ghost closed 8 years ago

ghost commented 8 years ago

I was running into an issue when running this behind a webserver as a microservice. Basically the first request would generate the pdf as expected however subsequent requests were returning the same PDF. After some debugging I found that the HTML files are being written correctly but options isn't getting updated.

Here is the options on the first pass:

{ 
    allowLocalFilesAccess: true,
    printDelay: 5000,
    settings: { javascriptEnabled: true },
    html: '<html>\n    <head></head>\n    <body>\n        <h1>blah</h1>\n    </body>\n</html>\n',
    footer: '<span style="float:left">Date Generated: 08-Sep-2016 12:17:36 (GMT -0500)</span>' 
}

And on the second:

{ allowLocalFilesAccess: true,
  printDelay: 5000,
  settings: { javascriptEnabled: true },
  footer: '<span style="float:left">Date Generated: 08-Sep-2016 12:20:28 (GMT -0500)</span>',
  viewportSize: {},
  paperSize: {},
  waitForJSVarName: 'PHANTOM_HTML_TO_PDF_READY',
  htmlFile: '/tmp/2337a2d0-75e8-11e6-9a97-dbf70888544fhtml.html',
  footerFile: '/tmp/2337a2d0-75e8-11e6-9a97-dbf70888544ffooter.html',
  url: 'file:///%2Ftmp%2F2337a2d0-75e8-11e6-9a97-dbf70888544fhtml.html',
  output: '/tmp/2337a2d0-75e8-11e6-9a97-dbf70888544f.pdf',
  html: '<html>\n    <head></head>\n    <body>\n        <h1>blah2</h1>\n    </body>\n</html>\n' }

Notice that the url is already set to the previous HTML document. And based on this code:

opt.url = opt.url || "file:///" + encodeURIComponent(opt.htmlFile);

It would reuse the same url over and over again.

My change just uses lodash to do a deep clone of the the options passed in so that any modifications that are done are not passed on to subsequent request.

pofider commented 8 years ago

Great, thanks.

pofider commented 8 years ago

Released in 0.4.6