Downloading to PDF or Preview and Save to PDF/Print using React

1.3k Views Asked by At

I need to know whats the better options between the two:

1st - I call backend to get the JSON response from it and use react-to-print package to preview and save to PDF/Print.

2nd - I call backend to get the JSON response from it and convert and download to PDF in frontend.

My problem with the 1st one if I have huge data, the browser lags for a long time. My question is, will the 2nd one be the same.

I know the best option would be the backend will be the one to convert to PDF - this is the 3rd option. I'm just asking if the 2nd is better than the 1st?

3

There are 3 best solutions below

3
On BEST ANSWER

In case both of it are force the browser to lags in my opinion the choise should be less evil. I think they won't have huge difference between. I'd follow the third way, just return pdf from the back-end.

2
On

I think it's better to do the work in the backend . and just use ajax to request the url ti download the pdf

3
On

It's an interesting question.

Both have there pro's & con's.

Backend: is likely the least trouble, and should not make the front-end lag. But the disadvantage is that your backend is now doing more work, so if you have 1000's of users doing this at the same time, it could put extreme load on your server. Also caching can be done with backend, eg. if you hashed the JSON, you could use this to help with caching at the client end, of course this only helps if multiple requests with identical JSON's are sent.

Frontend: This can more trouble, mainly down to browser compatibility, something you don't get with back end. But of course the advantage then is that the backend sever load is less. And to reduce lag client side, you could offload this to a Web Worker.

Another thing that's worth taking into consideration is Hosting costs,. Some hosting system can actually bill based on compute time, so in this case I would certainly do this client side.

In the end there is no right or wrong way, and is something you need to decide based on the above pro's & con's.