0

I use html2pdf to generate pdf from a wordpress post (inside a multi site install), it's working great I have the following setup:

on my homepage I have a link

<a target="_blank" id="downloadPDF" href="<?php echo get_template_directory_uri(); ?>/pdf_processor.php?blogid=<?php echo get_current_blog_id(); ?> ">download</a>

and on my pdf_processor.php the pdf is generated as espected and i use `

$html2pdf->Output('exemple.pdf', 'D');

using 'D' allow to download the pdf directly which is what i want.

now I would like to generate the pdf whith an ajax call (so i can display a loading icone while waiting), I already tryed setting up an ajax call with jquery that posts the get_current_blog_id(); to pdf_processor.php, i don't know how to handle the response so that when the ajax call is a success $html2pdf->Output('exemple.pdf', 'D'); is triggered, on my atempt i tried

echo $html2pdf->Output('exemple.pdf', 'D');

but that just returns a strange string.

  • You can't download binary with Ajax. Ajax is for text based stuff only, like XML and HTML. There is, however, a JQuery plugin to do something like what you want: see http://stackoverflow.com/questions/2186562/post-to-server-receive-pdf-deliver-to-user-w-jquery – developerwjk May 08 '14 at 22:22
  • Hi again, the plugin you suggested is working great, thanks for that. I still have a little problem. How can i want to append a spinning gif on click and remove it once the call return successful, From the plugin code examples I don't see how i can add a Callback to it to remove my spinning gif on success, any hint on that? Thanks – Lorenzo Pirondini May 09 '14 at 08:48

1 Answers1

2

Use 'F' to save the file on server and then open up the file. 'D' is always going to return some unreadable string if you try to alert it through javascript.

$html2pdf->Output('D:\xampp\htdocs\frescoframes12/My-File-Name.pdf','F');
HaveNoDisplayName
  • 8,291
  • 106
  • 37
  • 47
Anup Tilak
  • 336
  • 3
  • 15