1

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);
    });
});
Thomas Hughes
  • 99
  • 1
  • 11

1 Answers1

0

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.

Kevin Brown
  • 8,805
  • 2
  • 20
  • 38
  • Hi yes I meant to my own server. Thank you very much for the information, ill come back if I have any problems. :) – Thomas Hughes Oct 01 '17 at 07:31
  • The fiddle provided only outputs 'false' to the console log? I have tried on both Chrome and Safari. The same thing happens on my website too, but strangely it also appears to reload the page as the console log appears and then disappears immediately with the message: jquery.min.js:4 Resource interpreted as Document but transferred with MIME type application/pdf. Looking through the documentation on cloudformatter.com I can't see any option for 'base64' in render, and no matter which render I use it still downloads the document (even if i put 'none'). I have updated OP with my JS. – Thomas Hughes Oct 01 '17 at 07:55
  • You are correct, I am sorry. It is in the code but not what you are looking for. It base64 encodes the source information for debugging. I have modified answer with how edits to the JS could be made. – Kevin Brown Oct 02 '17 at 17:48