Giter Site home page Giter Site logo

Comments (5)

SebastiaanKlippert avatar SebastiaanKlippert commented on May 26, 2024

Hi,

That is a very good point. ResetPages only takes care of the input in the current implementation, but it could clear the output as well. The comment does indicate that is how you should use it, so you are correct.
I will update it later today, add a few tests and publish a new version.

from go-wkhtmltopdf.

SebastiaanKlippert avatar SebastiaanKlippert commented on May 26, 2024

I think I will just reset it every time it writes to the buffer on Create() or CreateContext() then it is always safe for reuse.

Also, I am a bit surpised with this behaviour on Lambda. We use it with Docker Lambda's as well but our code creates a new generator using wkhtmltopdf.NewPDFGenerator() for every PDF anyway so the buffer is not reused. Performance wise this is OK, the lookup of wkhtmltopdf on the system is the heaviest part and that is cached by the package internally in a global variable. So you could just create a new instance each time as a workaround.
Are using global variables to store the state between executions?

from go-wkhtmltopdf.

martinlindenberg avatar martinlindenberg commented on May 26, 2024

It is running on AWS lambda for me.

I think the reason why it behaves like this, is that i am only initiating the PDF Generator once per lambda container.
The main() function contains this line (and some additional configs...)
pdfg, err := wkhtmltopdf.NewPDFGenerator()
It is only called once per container initialization and keeps that object in memory.

The handler function uses the following function to render the pdf

func (h handler) renderPdf() ([]byte) {
  h.pdfgen.ResetPages()
  h.pdfgen.Buffer().Reset()                 // added

  h.pdfgen.AddPage(page)
  h.pdfgen.Create()
  return h.pdfgen.Bytes()
}

So it is not stored in global variables or anything - it just lives in the initiated containers memory.

from go-wkhtmltopdf.

SebastiaanKlippert avatar SebastiaanKlippert commented on May 26, 2024

Hi,

Thanks for explaining. In this case the PDF Generator object is indeed reused. Everything you declare in main is kept in memory as Lambda.Start() never returns, it just calls your handler (renderPdf()) for each invocation. Similar to http.ListenAndServe

To make the behaviour of this package more predictable the buffer is now cleared before each run where the output will be written to the buffer.
https://github.com/SebastiaanKlippert/go-wkhtmltopdf/releases/tag/v1.8.0

from go-wkhtmltopdf.

martinlindenberg avatar martinlindenberg commented on May 26, 2024

thanks for the fast reaction πŸ‘

from go-wkhtmltopdf.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    πŸ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. πŸ“ŠπŸ“ˆπŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❀️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.