How do I save the PDF to server? css2pdf@cloudformatter xeponline

781 Views Asked by At

I was wondering if anyone knew of a way to save the resulting PDF document to the server, instead of prompting the user to download it locally?

Using this: http://www.cloudformatter.com/CSS2Pdf

Many thanks

Edit: I am using the following JS to initiate the PDF.

$(function(){
    $('#generatePDF').click(function(e) {
        e.preventDefault();

        var pdfdata = xepOnline.Formatter.Format('printableInvoice',
            {
                pageWidth:'216mm',
                pageHeight:'279mm',
                render: 'base64'
            }
        );
        console.log(pdfdata);
    });
});
1

There are 1 best solutions below

3
On

Leaving the answer in place as the comments below are relevant. The original answer was how to get the source information (using the "base64" option), not the final PDF.

So to get the final PDF that is in memory, if you examine the code in Github:

https://github.com/Xportability/css-to-pdf/blob/master/js/xepOnline.jqPlugin.js

starting at the "else" at line 602 ... this "else" is executed if you force anything other than a download. If you chose "newwin" or "embed" as the method and the browser sniffing JS did not force it back to download (it does on Safari, IE and also mobile browsers), then this "else" is executed.

On a successful AJAX post, the function "xepOnline.Formatter.__postBackSuccess" is executed. This function starts at line 863. At line 865, the base64 encoded bytes of the actual PDF are loaded. If you debug your site and debug at that line of code, you can get the value of the var "base64" which will be the base64 encoded bytes.

So, if you only had Firefox and Chrome to consider, then you could do some mod to the code to post the result back to the server and not display it. If you have all those browsers to consider, you will need to add some option (like say option:'memory' which skips all browser sniffing, runs the AJAX version but with its own success function.

I can look at adding this to the library but you are free to pull it and make some mods yourself.