bigcapital icon indicating copy to clipboard operation
bigcapital copied to clipboard

[FIXED - Resolution Provided] PDF Generation Failure Across 4x Environments

Open df503 opened this issue 2 years ago • 5 comments

I have tried unsuccessfully to get both the docker production and docker development to produce PDFs.

Attempted to use the following OSs and versions: Debian 10, Debian 11, Ubuntu 22 and Ubuntu 24, Mac OS 13.5.1 (ARM) and Mac OS 13.5.1 (Intel).

Everything else seems to work, just not PDF generation. This is not the first thread on the topic, there is a thread on Discord with no resolution.

docker logs bigcapital-server show:

root@bigcapital:/etc/bigcapital# docker logs bigcapital-server (node:1) [MONGODB DRIVER] Warning: Current Server Discovery and Monitoring engine is deprecated, and will be removed in a future version. To use the new Server Discover and Monitoring engine, pass option { useUnifiedTopology: true } to the MongoClient constructor. (Use node --trace-warnings ... to show where the warning was created)

  ################################################
    Server listening on port: 3000
  ################################################

(node:1) UnhandledPromiseRejectionWarning: Error: ENOENT: no such file or directory, open '/app/packages/resources/views/modules/invoice-regular.pug' at Object.openSync (fs.js:498:3) at Object.readFileSync (fs.js:394:35) at handleTemplateCache (/app/packages/server/node_modules/pug/lib/index.js:241:37) at Object.exports.renderFile (/app/packages/server/node_modules/pug/lib/index.js:454:10) at Object.templateRender (/app/packages/server/build/index.js:101267:26) at SaleInvoicePdf.saleInvoicePdf (/app/packages/server/build/index.js:86089:37) at processTicksAndRejections (internal/process/task_queues.js:95:5) at async Object.application/pdf (/app/packages/server/build/index.js:12949:40) (node:1) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). To terminate the node process on unhandled promise rejection, use the CLI flag --unhandled-rejections=strict (see https://nodejs.org/api/cli.html#cli_unhandled_rejections_mode). (rejection id: 1) (node:1) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.

I can temporarily resolve this issue by getting shell access to the bigcapital-server container and fixing the incorrect file path by copying the files /app/server/resources to /app/resources with (cp -R /app/packages/server/resources /app/packages), but that obviously gets wiped out and is not a persistent fix. So the first error has a known resolution.

Then when I try to generate a PDF again, I get the following new error in the docker container log:

(node:1) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code. (node:1) UnhandledPromiseRejectionWarning: Error: Exactly one of browserWSEndpoint, browserURL or transport must be passed to puppeteer.connect at Object.assert (/app/packages/server/node_modules/puppeteer/lib/cjs/puppeteer/common/assert.js:26:15) at Object.connectToBrowser (/app/packages/server/node_modules/puppeteer/lib/cjs/puppeteer/common/BrowserConnector.js:57:17) at PuppeteerNode.connect (/app/packages/server/node_modules/puppeteer/lib/cjs/puppeteer/common/Puppeteer.js:50:38) at PuppeteerNode.connect (/app/packages/server/node_modules/puppeteer/lib/cjs/puppeteer/node/Puppeteer.js:83:22) at PdfService.pdfDocument (/app/packages/server/build/index.js:70598:51) at SaleInvoicePdf.saleInvoicePdf (/app/packages/server/build/index.js:86090:50) at runMicrotasks () at processTicksAndRejections (internal/process/task_queues.js:95:5) at async Object.application/pdf (/app/packages/server/build/index.js:12949:40) (node:1) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). To terminate the node process on unhandled promise rejection, use the CLI flag --unhandled-rejections=strict (see https://nodejs.org/api/cli.html#cli_unhandled_rejections_mode). (rejection id: 3)

Then I get an error about Browser WSEndpoint which the source code references an environment variable of BROWSER_WS_ENDPOINT but I don't find any examples of the value in the .env.example file. I tried BROWSER_WS_ENDPOINT=ws://localhost as a random guess, but that didn't work.

src/config/index.ts has this line reference to it.

/**

  • Puppeteer remote browserless connection. */ puppeteer: { browserWSEndpoint: process.env.BROWSER_WS_ENDPOINT, },

df503 avatar Sep 04 '23 15:09 df503

I found the solution which is 3 fold.

  1. There is an extra directory escape (../) that goes 1 directory higher than it should be after the Dockerfile has run and moved the resources into their place within the docker image /app folder. Here is the updated path for the template files used for the PDFs. I've never formally submitted an edit to any public GitHub project.
image
  1. Second, the puppeteer launch of Chromium needs to specify the path for the chromium-browser executable and in order to run a headless browser, we need to send some additional arguments to address the permissions for running chromium.

Updated packages/server/src/services/PDF/PdfService.ts (see screenshot): image

  1. Last but not least, the packages/server/Dockerfile needs to have the Chromium prerequisites added before running the puppeteer installation otherwise the chrome executable will result in an odd "not found" error when the file is clearly there with an ls -l.
image

If screenshots aren't the correct search engine friendly method of sharing code updates, let me know. I figured it might be easier to read rather than mixed code and description of what's happening.

df503 avatar Sep 14 '23 04:09 df503

This issue will be fixed after https://github.com/bigcapitalhq/bigcapital/issues/207 it's being added to the next sprint.

abouolia avatar Sep 28 '23 18:09 abouolia

If screenshots aren't the correct search engine friendly method of sharing code updates, let me know. I figured it might be easier to read rather than mixed code and description of what's happening.

The best way is to make a PR @df503 :)

technowhizz avatar Oct 01 '23 02:10 technowhizz

@abouolia Any Idea when #207 will be completed as this feature (#235) is quite an important feature that is broken?

technowhizz avatar Oct 11 '23 01:10 technowhizz

I found the solution which is 3 fold.

  1. There is an extra directory escape (../) that goes 1 directory higher than it should be after the Dockerfile has run and moved the resources into their place within the docker image /app folder. Here is the updated path for the template files used for the PDFs. I've never formally submitted an edit to any public GitHub project.
image

@df503 Invalid runtime __dirname is caused by webpack

Error: ENOENT: no such file or directory, open '/myworkspace/bigcapital/packages/resources/views/modules/receipt-regular.pug'

Add node: { __dirname: true },to webpack config may be better. https://github.com/bigcapitalhq/bigcapital/blob/151aff4c8e481f286cf1017573c50d8844cc6ec4/packages/server/scripts/webpack.common.js#L16

after adding node option it works, __dirname outputs: src/utils

magicxie avatar Nov 28 '23 09:11 magicxie