How to use pdfkit npm in async manner

888 views Asked by At

I have written an application in node.js which takes input from user and generates pdfs file based on few templates.

I am using pdfkit npm for this purpose. My application is running in production. But my application is very slow, below are the reasons :

What problem I am facing :

  • It is working in sync manner. I can explain it by giving an example- Suppose a request come to the application to generate a pdf, is starts processing and after processing it returns back the response with generated pdf url. But if multiple request comes to the server it process each request one by one(in sync manner).
  • All request in queue have to wait untill the previous one is finished.
  • Maximum time my application gives Timeout or Internal Server Error.

I can not change the library, why ?

  • There are 40 templates I have written in js for pdfkit. And each template is of 1000 - 3000 lines.
  • If I will change the lib, i have to rewrite those templates according to new library.
  • It will take many months to rewrite and test it properly.

What solution I am using now :

  • I am managing a queue now, once a request come it got queued and a satisfactory message send back in response to the user.

Why this solution is not feasible ?

  • User should be provided valid pdf url upon success of request. But in queue approach, user is getting only a confirmation message. And pdf is being processed later in queue.

What kind of solution I am seeking now ?

  • Any way through which I can make this application multi-threaded/asynchronous, So that it will be capable of handling multiple request on a time without blocking the resource?

Please save my life.

1

There are 1 answers

0
Sam Bobel On

I hate to break it to you, but doing computation in the order tasks come in is a pretty fundamental part of node. It sounds like loading these templates is a CPU-bound task, and since Node is single-threaded, it knocks these off the queue in the order they come in.

On the other hand, any framework would have a similar problem. Node being single-threading means its actually very efficient, because it doesn't lose cycles to context switching.

How many PDF-generations can your program handle at once? What type of hardware are you running this on? If it's failing on a few requests a second, then there's probably a programming fix.

For node, the more things you can make asynchronous the better. For example, any time you're reading a file in, it should be asynchronous.

Can you post the code for one of your PDF-creating request functions?