I have an export to PDF feature in an app that emails financial data to a user. I got this feature 100% working using my test site, but it did not work when I deployed to my production site. On the prod site, the user is emailed with a blank, 1kb, 1-page PDF file (this is open-able in a PDF viewer).
Here's the down-low for how I have the feature working. I used this guide to get Nightmare JS working on Amazon Linux.
- Server export receives an
/export
call A child process is called that uses
xvfb-run
to create a visual frame buffer for Nightmare JS to work off of.exec(`xvfb-run -a --server-args="-screen 0 1366x768x24" node ${path.join(__dirname, 'export.js')} ${process.env.TYPE} ${token} ${req.user.email}`)
In the export.js process, Nightmare JS does an HTTP browser load off of the port that the site is running off of (test site: port 3001, prod site: 3000).
Nightmare() .viewport(1366, 768) .goto( `http://localhost:${process.env.PORT}/export_summary_1? exportToken=${exportToken}` ) .wait(3000) .pdf() .end() .then(function(pdfBuffer) { mailOptions.attachments = [{ filename: 'financial_model.pdf', content: pdfBuffer }] email.send(mailOptions) })
The
/export_summary_1
browser page makes another server call to load and display the financial data using the export token. Then, as seen in the code above, Nightmare captures the page into PDF, and finally this PDF is emailed to the user.
I don't believe my AWS setup is the culprit because the concerned portion of the export to PDF system is all done locally, on the AWS instance (e.g.-the Nightmare page load is done to http://localhost:[port]/export_summary_1
).
I do have an HTTP->HTTPS redirect, but I have this bypassed for the Nightmare page load and the loaded page's server call to retrieve the financial data.
Even though I don't believe the issue is my AWS setup, here's my notes on the AWS and port setup, for completeness.
- Both ports 3000 and 3001 are enabled in my security group used by my instance.
- Load balancer setup
- Port 80 and 443 listeners forward to 'ec2-instance-targets', which has the AWS instance as a registered target, using port 3000.
- Port 3001 listener forwards to 'ec2-devInstance-targets', which has the AWS instance as a registered target, using port 3001.
Also, the AWS instance is an 'unhealthy' registered target in the 'ec2-instance-targets' view('ec2-instance-targets' is used for the prod site). This is because, I believe, the 'ec2-instance-targets' target group has a port setting of 80 while the registered target has a port setting of 3000.
But, the prod site still works, and as I said before, these AWS/load balancer settings don't seem to be the issue source because the export to PDF system is contained within the AWS instance, for all intents and purposes.
Although, I do plan on trying to resolve the above aspect in a few hours, when it will be nighttime for me.
Also, I commented out the Nightmare-loaded page's call to the server for financial data. Now, the loaded page that is exported to PDF just has 'hello world' text on it. The issue persists.
How can I get the export to PDF feature working on the prod site?