In drupal 7, when I load a pdf page in a new tab, and after reloading the initial page, I have this warning:
Warning : Cannot modify header information - headers already sent in invoices_callback() (line 1093 in C:\xampp\htdocs\drupal\sites\all\modules\custom_profile\custom_profile.module).
Warning : Cannot modify header information - headers already sent in drupal_send_headers() (line 1221 in C:\xampp\htdocs\drupal\includes\bootstrap.inc).
Actually, I have a link in my initial page (named 'my-account/invoices') which redirects to a PDF page named 'my-account/invoices/%invoice_number'. This last one is loaded by a 'page callback' function :
function invoices_callback($var){
require_once('sites/all/libraries/dompdf/dompdf_config.inc.php');
$filename = 'Invoice n°' . $var . '.pdf';
$dompdf = new DOMPDF();
$dompdf->load_html('<div>Test n° : ' . $var . '</div>');
$dompdf->render();
$dompdf->stream($filename, array('Attachment'=>0));
header('Content-type: application/pdf');
}
As you see, I didn't use a pdf module, like printer, which is too heavy. I use a custom module which implement a page callback function, and inside I use dompdf library.
The pdf is displayed normally, but when I come back to initial page I have these warnings.
I have tried several things:
- I removed the header function. Effect: the first warning is gone but I still have the second. It seems like the stream method of DOMPDF class wants to sent a header...
- I have added a charset utf-8 to the header function
- I used
ob_start()
andob_end_flush()
functions at the beginning and at the end of the code. Surprisingly doesn't work. - I lastly use drupal_add_http_header function. Doesn't remove warnings.
What i can do is to call a script in an external link, because my test work when i call a php script located elsewhere. But for the UX, i try to have all my scripts managed by drupal, and i want the invoices to be called by a simple alias url (my-account/invoices/%invoice_number).
So my question is simple: How to avoid these warnings without moving my script to an external location?