Converted PDF should be saved on server side, not downloaded - javascript

The issue is that a converted PDF is downloaded by the client. I need the PDF to be saved on server side and sent by mail.
I am using jsPDF and html2canvas for converting HTML to PDF.

I guess that jsPDF is not the proper tool for your task. First line at Github is "Client-side JavaScript PDF generation for everyone" which says it all.
To let the server create a PDF, you need a tool which renders the HTML result and creates a PDF from it. Maybe a MS Word or OpenDocument template used in headless mode will suit your needs, as suggested here among other solutions.

Related

How can I get all the text in Chrome's PDF viewer in Javascript?

I am writing a Chrome extension that manipulates PDF files and am looking to get all the text of a PDF file that's currently open in Chrome's PDF viewer.
I learned from
How can I get selected text in pdf in Javascript?
how to get selected text, but I couldn't find in the API a function that extracts the entire text (or better yet the PDF itself, and then send it to a server). Is it possible?
I am aware of the solution of sending the URL and downloading it on the server side, but sometimes it is problematic (e.g. PDFs from password-protected websites).
Thanks.
I have tried looking in the API, https://source.chromium.org/chromium/chromium/src/+/main:chrome/browser/resources/pdf/pdf_scripting_api.ts
I have also tried "downloading" the PDF on client side and sending it as Blob to a server, but it's a problematic solution (sometimes blocked for example, requires extra permission).

Angular 8 - PDF generation using jsPdf

Im using jsPdf to generate a PDF document which captures my html screen contents. I can see my downloaded PDF file in the download folder.
Can we change the path where the PDF is getting downloaded? Instead of having the default c:/downloads can we change the path?
Answer to your question is NO. It is not possible to change default download folder from Angular/Javascript or any other framework/language for that matter. It is forbidden due to user's security. That decision is always handled by the browser.
You could however define a Content-Disposition header that tells the browser if the file is supposed to be displayed inline or if it an attachment that can be downloaded and saved locally.

Most used approach to generate a PDF report (JavaScript, node.js)?

Can anyone who worked on something like this describe the general process? I'm very confused right now. By report I mean a visually appealing document with logo, tables, headers and footers, and the data will be retrieved dynamically.
The approaches I looked at are:
Use a server side library (node.js module) that generates the PDF. Send the string representation as response with Content-Type: application/pdf.
Problem: I chose PDFKit, but it doesn't work and no content shows up at all. It uses PDF 1.3, which is old.
Generate PDF on client side.
Problem: Most popular library seems to be jsPDF, but it's not very capable of producing sophisticated-looking documents.
Write template in PDF source code and fill in the data on server side.
Problem: The encoding is weird, for example if I just do doc.text("1"), a lot of unrecognizable characters appear for just the string "1". I'm very confused about this.
Finally, it'll be super helpful if anyone provides a link that can help me understand the encoding! It's super confusing to me.
Any experience with similar tasks is much appreciated!
I haven't personally done this, but the first thing I would try would be:
On the server side dynamically build the appropriate HTML document and CSS
Use phantomJS to render that document
Tell phantomJS to convert that document to PDF, saved in a temp file
Send the PDF back as the HTTP response by writing the temp PDF file to the response body
Delete the temp file
If you are struggling with content-type, content-disposition, etc, you shouldn't have to worry about that if you have a valid pdf file on disk and just write that data to the HTTP response. With the right headers, you should be able to ask the browser to either display the PDF or treat it as a file to be saved as a download.
As the member of jsreport team, I would give it a shot.
jsreport platform provides multiple ways how to generate pdf reports. The most common included one is to transform html into pdf using headless chrome. jsreport will also compile and render handlebars or jsrender html templates if its specified, it can embed images, add header/footer, run custom javascripts and more.
You can play with the examples and see the options you have
https://playground.jsreport.net
When you are done with playing, you can use jsreport online or download and install jsreport server to your company. Then you are ready to call its REST API and generate reports.
More to your question
jsreport will provide correct content-type in the response for pdf or html. You can just let the browser to display the result
data can be sent to jsreport api or retreived by custom script
I'm not sure what the most common approach is, but personally I like to create an HTML template, populate it in my server-side code, and then use wkhtmltopdf to convert the HTML into a PDF. If you're using .NET you should check out WkHtmlToXSharp (which is a .NET wrapper for wkhtmltopdf.)

How to print outputstream PDF using Javascript

I'm getting PDF file as output stream and want to print it using Javascript. There're some tips about printing PDF file or iframe. But I need to print PDF files that is generated by server after print request.
JavaScript can't access any local resources like the file system or attached printers, so this isn't possible directly.
What you need is to convert the PDF to, say, HTML5. Then display that in your browser and call document.print() to print it.
The project pdf.js already does a pretty good job to convert PDF to HTML.

Save HTML5 page as PDF

On click of a button, without doing a round trip to the server, can we save a HTML5 page on clients machine as PDF.
Check out PDF.js. This lib can create pdf in the browser (or serverside on node.js).
Using chrome you can print the page to a pdf file.
You need something like this, maybe server-side you can start a browser and print the content to a pdf file and send this to the client.
Disclaimer: i work for ByteScout
If you have simple HTML formatting and want to generate PDF on client side and if you have non-commercial project, check BytescoutPDF.js - it supports simple HTML formatting for text (font name, size, color) plus simple drawings and images, should be enough for simple reports

Categories