Я использую TCPDF в Drupal 7. Ниже мой код:
function pdf_download($client_id)
{
$styles[] = array(
'type' => 'file',
'media' => 'all',
'data' => pdf_css_path(),
'group' => CSS_DEFAULT,
'every_page' => false,
'weight' => 0,
'preprocess' => false,
'browsers' => array(),
);
$html1 = $html2 = drupal_get_css($styles);
$booking = entity_load_single('booking_summary', $client_id);
$booking_summary_info = booking_summary_details($booking, false);
$html1 .= theme('space_booking_summary_pdf_pg1', array('info' => $booking_summary_info, 'order' => $booking));
$html2 .= theme('space_booking_summary_pdf_pg2', array('info' => $booking_summary_info, 'order' => $booking));
// Initialize pdf instance
$tcpdf = tcpdf_get_instance();
// Assign header and footer
$tcpdf->DrupalInitialize(array(
'footer' => array(
'html' => '<div style="text-align: center;"><strong>This is a computer generated pdf.</strong></div>',
),
'header' => array(
'callback' => 'booking_summary_pdf_header',
),
));
// Write html to pdf
$tcpdf->writeHTML($html1, true, false, true, false, '');
// add a page
$tcpdf->AddPage();
$tcpdf->writeHTML($html2, true, false, true, false, '');
// reset pointer to the last page
$tcpdf->lastPage();
// Download pdf
$bk_created = date('j F Y', $booking_summary_info['created']);
$filename = "BK{$booking_summary_info['booking_id']}";
$filename .= "_" . str_replace(' ', '_', $booking_summary_info['billing_user']['fullname']);
$filename .= "_" . str_replace(' ', '_', $bk_created);
// Supply a filename including the .pdf extension
$filename = "{$filename}.pdf";
// Change the path to whatever you like, even public:// will do or you could also make use of the private file system by using private://
$uri = 'public://tmp/';
$stream = file_stream_wrapper_get_instance_by_uri($uri);
$path = DRUPAL_ROOT . '/' . $stream->getDirectoryPath() . '/' . file_uri_target($uri);
// Create the full path
$full_path = "{$path}/{$filename}";
$tcpdf->Output($full_path, 'I');
}
Тот же код генерирует правильный pdf при выполнении на локальном компьютере, но не генерирует pdf при работе. Нет ошибок ни в логах Drupal, ни в логах nginx.
Вывод в браузере Chrome при выполнении на производстве выглядит примерно так, как показано ниже:
В чем может быть причина такого странного поведения?
РЕДАКТИРОВАТЬ: Вышеупомянутое поведение присутствует только в Chrome. Другие браузеры правильно отображают PDF-файл.