Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Is there a way to add custom writer for output? #482

Open
hirenchauhan2 opened this issue Oct 9, 2024 · 5 comments
Open

Is there a way to add custom writer for output? #482

hirenchauhan2 opened this issue Oct 9, 2024 · 5 comments
Labels
in analysis Analyzing if should be implemented new feature New feature or request v2 will be solved in v2

Comments

@hirenchauhan2
Copy link

hirenchauhan2 commented Oct 9, 2024

Is your feature request related to a problem? Please describe.
I'm trying to create a workflow to generate PDFs with QR Codes in the AWS Lambda runtime. The data is fetched from DB and gets passed to the maroto for creating grid of QR codes on pages, once the whole thing is ready then only we can get the document to get the raw byte which we can upload to S3. Now, this can get pretty slow if the data volume is larger and for serverless environments this is not a very good case.

The problem with this approach is that if the file content([]byte) is too big, we're not able to upload such big data with S3's PutObject command. we need to use multipart upload for such large payloads. So in lambda function we can't do such big operation where we're constrained with memory, execution time, space limitations.

Can we get a way where we can start the upload from the moment we create the instance of of maroto and attach a writer to it which will be called whenever any data is written to it, so that I can implement a custom writer that can upload this data to S3 bucket without having to load the whole bytes list and then pass this huge list for upload and reduce the temporary file storage on the runtime where the code is being executed.

I've tried the package go-pdf/gpdf for generating the pdf file and setting the custom output writer which uploads the file to S3 with multipart uploads. But only problem with that library is that it generates a very larger file due to QR codes in it. I tried to compress the QR codes buffer but did not get the good results with that either. Then I switched to Maroto, which give the QR code generation with it so one dependency less for generating QR codes explicitly and it also generated the pdf file with less size. Maybe I was doing something which I shouldn't be doing in the go-pds/fpdf package that would have increased the size.

If someone knows how to reduce the final size, I would appreciate your help.

Describe the solution you'd like
I want a solution where I can add the target where the bytes are going to get written to. Instead of creating the document by using m.Generate(), I want something like this:

// ... config skipped for breavity
m := maroto.New(cfg)
m.SetOutput(&myWriterInstance)
m.AddRows(...) // as soon as the rows are added the data is written to the writer object

Maybe if there's something already present in the docs which I have missed, please let me know.

Describe alternatives you've considered
I've tried using the go-pdf/fpdf with skip2/go-qrcode to generate pdf with qr codes in it. the issue with that approach was the size of generated output pdf file, and slower generation as the data got larger.

I tried maroto to generate the pdf file for about 98K records and it was running for about 2Hrs and still did not finish writing the file to disk. Is there anything we can do to overcome this issue?

Additional context
N/A

@hirenchauhan2 hirenchauhan2 changed the title Is there a way to upload the pdf document as stream as soon as it gets some data written to it? Is there a way to add custom writer for output? Oct 9, 2024
@johnfercher johnfercher added new feature New feature or request in analysis Analyzing if should be implemented v2 will be solved in v2 labels Oct 13, 2024
@johnfercher
Copy link
Owner

Hello, the go-fpdf which we use support the usage of io.Writer, this would solve your issue?

@hirenchauhan2
Copy link
Author

@johnfercher how do I add that in the maroto's flow of document generation? Could you please share any example of that if possible?

Thank you.

@johnfercher
Copy link
Owner

johnfercher commented Oct 14, 2024

See this: https://pkg.go.dev/github.com/jung-kurt/gofpdf#Fpdf.Output, what maroto does is to pass a b = bytes.Buffer to it, and executes a b.Bytes() and return. Is possible to expose this io.Writer to you handle it outside maroto. This would solve your issue?

@hirenchauhan2
Copy link
Author

Let me check and get back to you on this. I appreciate you responding so nicely. Thank you so much for the support.

Though my problem is over now since the project does not require the feature. I will try it on my own and let you know if that works or not.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
in analysis Analyzing if should be implemented new feature New feature or request v2 will be solved in v2
Projects
None yet
Development

No branches or pull requests

3 participants
@johnfercher @hirenchauhan2 and others