You are here

function _print_pdf_wkhtmltopdf in Printer, email and PDF versions 7

Same name and namespace in other branches
  1. 5.4 print_pdf/print_pdf.pages.inc \_print_pdf_wkhtmltopdf()
  2. 6 print_pdf/print_pdf.pages.inc \_print_pdf_wkhtmltopdf()
  3. 5.x print_pdf/print_pdf.pages.inc \_print_pdf_wkhtmltopdf()

Generate the PDF file using wkhtmltopdf

Parameters

$print: array containing the configured data

$html: contents of the post-processed template already with the node data

$filename: name of the PDF file to be generated

See also

print_pdf_controller()

1 call to _print_pdf_wkhtmltopdf()
print_pdf_generate_html in print_pdf/print_pdf.pages.inc

File

print_pdf/print_pdf.pages.inc, line 365

Code

function _print_pdf_wkhtmltopdf($print, $html, $filename = NULL) {
  $print_pdf_pdf_tool = variable_get('print_pdf_pdf_tool', PRINT_PDF_PDF_TOOL_DEFAULT);
  $print_pdf_paper_size = variable_get('print_pdf_paper_size', PRINT_PDF_PAPER_SIZE_DEFAULT);
  $print_pdf_page_orientation = variable_get('print_pdf_page_orientation', PRINT_PDF_PAGE_ORIENTATION_DEFAULT);
  $print_pdf_content_disposition = variable_get('print_pdf_content_disposition', PRINT_PDF_CONTENT_DISPOSITION_DEFAULT);
  $print_pdf_wkhtmltopdf_options = variable_get('print_pdf_wkhtmltopdf_options', PRINT_PDF_WKHTMLTOPDF_OPTIONS);
  $dpi = 96;
  if (!empty($print_pdf_wkhtmltopdf_options)) {
    $print_pdf_wkhtmltopdf_options = token_replace($print_pdf_wkhtmltopdf_options, array(
      'node' => $print['node'],
    ), array(
      'clear' => TRUE,
    ));
  }
  $version = _print_pdf_wkhtmltopdf_version();

  // 0.10.0 beta2 identifies itself as 0.9.9
  if (version_compare($version, '0.9.9', '>=')) {
    $print_pdf_wkhtmltopdf_options = '--disable-local-file-access ' . $print_pdf_wkhtmltopdf_options;
  }
  elseif (version_compare($version, '0.9.6', '>=')) {
    $print_pdf_wkhtmltopdf_options = '--disallow-local-file-access ' . $print_pdf_wkhtmltopdf_options;
  }
  else {
    drupal_goto($print['url']);
    exit;
  }
  $descriptor = array(
    0 => array(
      'pipe',
      'r',
    ),
    1 => array(
      'pipe',
      'w',
    ),
    2 => array(
      'pipe',
      'a',
    ),
  );
  $cmd = '"' . realpath($print_pdf_pdf_tool) . "\" --page-size {$print_pdf_paper_size} --orientation {$print_pdf_page_orientation} --dpi {$dpi} {$print_pdf_wkhtmltopdf_options} - -";
  $process = proc_open($cmd, $descriptor, $pipes, NULL, NULL);
  if (is_resource($process)) {
    fwrite($pipes[0], $html);
    fclose($pipes[0]);
    $pdf = stream_get_contents($pipes[1]);
    fclose($pipes[1]);
    stream_set_blocking($pipes[2], 0);
    $error = stream_get_contents($pipes[2]);
    fclose($pipes[2]);
    $retval = proc_close($process);
    if (!empty($error) || $retval != 0) {
      if (empty($error)) {
        $error = 'No stderr output available.';
      }
      watchdog('print_pdf', 'wkhtmltopdf [%cmd] (returned %ret): %error', array(
        '%cmd' => $cmd,
        '%ret' => $retval,
        '%error' => $error,
      ));
    }
  }
  if (!empty($pdf)) {
    if ($filename) {
      if (headers_sent()) {
        exit("Unable to stream pdf: headers already sent");
      }
      header("Cache-Control: private");
      header("Content-Type: application/pdf");
      $attachment = $print_pdf_content_disposition == 2 ? "attachment" : "inline";
      header("Content-Disposition: {$attachment}; filename=\"{$filename}\"");
      echo $pdf;
      flush();
      return TRUE;
    }
    else {
      return $pdf;
    }
  }
  else {
    drupal_set_message(t('Unable to generate PDF file.'), 'error');
    drupal_goto($print['url']);
    return NULL;
  }
}