Saving pdf on server side with jsPDF - javascript

I have an application that generates a HTML page with data which the user can edit.
At the end I generate a .pdf file with jsPDF.
Is there any way that I can save this generated .pdf on my server-side database?
I'm using PrimeFaces.
Thanks in advance

Updating my solution for other users:
I found the .output('datauristring') method of the jsPDF which returns me a BASE64 String.
Then the String is sent via JSON to my backed bean and converted as my will.

Thanks all for the help! I've found my solution:
Once the user presses on the button to generate the .pdf, I'll save all the data he filled on the database, just the data.
When the user wants to see the pdf he generated back, I'll generate a new one with the data collected on his first submit.
Thank you all for the answers, they were helpful.

I suggest to use wkhtmltopdf.
It is a open source cross platform command line tool to generate pdf files from html content.
So to your requirement what you can do is, you need to send the html content to the server and save this content into a html file.
Once you saved html content into html you can call wkhtmltopdf like you execute MS-DOS commands in java.
Example: new ProcessBuilder("wkhtmltopdf.exe", htmlFilePath, pdfFilePath);
Once pdf file is generated you can read and store into database.

Related

Angular : Save editable PDF data using angular/javascript?

Html:
<iframe [src] ="fileurl" #iframe>
</iframe>
<button (click)="saveDoc()">
</button>
Stuck at savedoc() functionality.
Typescript:
/*api call to get document blob data on load*/
var fileurl = new blob([res.body],{type:application/pdf});
fileurl = URL.createObjectUrl(fileurl);
savedoc(){//here need logic to access edited pdf content as blob }
I am able to view and write in pdf editable fields (as input or checkboxes) but I can't figure out once filled all details, how to save/access that edited PDF content (mostly in blob format) to send back to server when click on save button. I have also tried ng2-pdf-viewer library of npm but same issue. I have to send this edited pdf to server in blob format to replace with existing.
How can I access edited pdf content?
Edited: Alternative approach, if its possible to trigger saveAs event from code to save iFrame pdf in local drive? I am using Window.showSaveFilePicker();but saved file seem corrupted or not exist.
Have a look at PDF-LIB.
It is a great JavaScript library which provides all sorts of tools to manipulate PDF documents. There is even tooling for filling the fields and saving the newly filled PDF.
In a past project, I used this library to capture user information from an HTML form and have it inserted and saved into a PDF.
Note:
Remember that once you have the filled PDF on client side, you must send it back to server side to update the PDF that is stored on the server.

show msg in modal in react or javascript

I have an interesting question:
I give the user the option to upload .MSG files to the system,
Inside the DB I keep the BASE64 of the file
Now I want to introduce the .MSG inside a model
And here I am having a problem
I'm unable to convert BASE64 back
I will note that I can see extensions of PDF and WORD
Using the PDFVUWER library...
I do not have a code because I do not know how to do it
Are you asking how to open an MSG file in JS? You'd need to use Outlook Object Model (Application.Session.OpenSharedItem), but that means JS would only be able to do that in IE.
JavaScript doesn't provide anything for drawing MSG files on the page. You need to use Outlook for opening such files if you want to display them to users. But that would be in Outlook (external application), not in the browser. Moreover, Outlook should be installed on the client side.
Instead, I'd recommend keeping the message body represented by the HTMLBody property value from the OOM in the Db. In that case you will be able to display it to a user like a regular web page. Note, the message body requires some processing before saving to the Db, so embedded images could be displayed.
When MSG files are uploaded to the application you can extract the required pieces of the file such as message body and etc. for displaying it at any point later. The MSG file format is described in the Outlook Item (.msg) File Format section of MSDN.

Generating HTML file (or Zip with html inside) for user to download

I want to create a website, where user fills out the form and after submitting I want to generate a html file for him with the data he filled and then allow him to download this file.
Is it possible with JS without server side JS?
You can create a link where href property is an encoded URI.
let exampleText ="My Name\nMy Surname\nMy Town\n"
let localfile = "data:text/plain;charset=utf-8," + encodeURIComponent(exampleText);
document.getElementById("linkfile").setAttribute("href", localfile);
<a id="linkfile" download="myInfo.txt">Click here to download</a>
If you want to do it JUST frontend side. You could use zip.js to convert those files into a zip, then convert it into a binary array, then convert it into a blob file, and prompt for download using this question.
You may have to casts your html files into DOM "File"s, this might be very slow on mobile since all processing will be done on client side.

Proper way to export HTML table as Excel file?

What's the correct way to export an HTML table as an Excel file so that the user can click a button and download the Excel file (ideally using Angular and without using server)?
I've seen many answers like this:
Export to xls using angularjs but doing this gives an error similar to the following:
"The file format and extension dont match... The file could be corrupted..."
and I believe the file is actually in HTML or XML format, not actual Excel.
The warning does not present a good image to the user.
What's the right way to actually export a file as Excel without using the server?
Or is the server required to create the file?
If you are just using tabular data, then I would argue that the best solution would be building a CSV file. This could be natively opened by excel and converted into an XLS file if necessary. You can do so by arranging your data with a data URI. The octet-stream will force a file download rather than opening in browser. Here is an example:
CSV

Download php genereted csv file though jquery/javascript

I can download a csv file from php, but I need to do it via jquery/javascript.
what would be the best solution for that? how to do it?
Save php generated csv file in tmp dir and download via jquery/JS but how?(app used internally)
Send array of data back to frontend, do all in jquery/JS.
Actually I want to update data on page and same time download changed rows!
I can not use any plug-in of any kind.
EDIT:
Ok! HERE is the full problem, i forget to mention.
I have a page with some rows from DB,
I wanted to update selected rows in DB
Same time get changed values and update rows on page
download changed rows in csv
In one button click,
I cant use different buttons to save data and download file because after updating rows in db how would I get last updated rows?
With my limited knowledge, I can just get json response and update page Or download file not both!
So here is how i solved it :)
I made csv string out of json returned data in JS.
and used data URI scheme like here:
I made a link and that downloads .csv file.
var uri = 'data:application/csv;charset=UTF-8,' + encodeURIComponent(csv_ouput);
$('#csv-btn-div').append('Download CSV');
There is another solution, if you want file save dialog to appear!
Thanks All!
$.get("/someCsv.php", function(data) {
// do stuff here.
});
Make sure to use some form of caching at the server side.
I would prefer preprocessed data (json):
$.get("/createJson.php", function(data) {
// do stuff here.
});

Categories

Resources