2

I would like to generate a pdf with wkhtmltopdf at the end of my batch.

I integrate the generation in the callback function but it does not offer me the download of the file

This is my finished function:

function csm_generate_pdf_finished($success, $results, $operations) {
  $message = 'some errors';

  if ($success) {
    if (($library = libraries_load('phpwkhtmltopdf')) && !empty($library['loaded'])) {
      $pdf = new WkHtmlToPdf();
      $html = $results['data'];
      $start = $results['start'];
      $end = $results['end'];
      $pdf->addPage( $html );

      $filename = 'document_'.$start.'_'.$end.'.pdf';

      $pdf->send($filename);

      $message = 'Generation PDF ok';
    }
  }
  drupal_set_message($message);
}
2
  • Does this code work when using it outside of batch?
    – No Sssweat
    Oct 31, 2019 at 11:38
  • Yes it works outside
    – Kipmag
    Oct 31, 2019 at 13:08

1 Answer 1

1

The Finish function runs in the background and you can't return file there, the solution for your case is redirected to another page and create and return pdf file there.

something like drupal_goto('your_custom_url_to_generate');

7
  • I try to add drupal_goto($path) with $path = '/generate/'.$start.'/'.$end; it works, my file is downloaded, but the progress bar is stuck
    – Kipmag
    Oct 31, 2019 at 13:45
  • When you redirect to a download, there is no change to the current page HTML. I would create a page with a download link in it, redirect to that and use some JS on the page to trigger the download automatically. Oct 31, 2019 at 14:36
  • @Kipmag, have you cleared the cache? maybe you need do some more action after downloading
    – Yuseferi
    Oct 31, 2019 at 15:16
  • Yes, I cleared cache, but nothing
    – Kipmag
    Oct 31, 2019 at 15:26
  • @Kipmag what if you redirect to another page after that?
    – Yuseferi
    Oct 31, 2019 at 15:28

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.