I’ve built a webservice in a Drupal Module. My issue is not related to Drupal or webservice. It works fine.
User fills a form, submits and is connected to the webservice.
When user is connected to this webservice, I get a PDF source code that I store in a variable : $result
If I display the content of $result
for testing purpose, the PDF source code is correct.
echo '<pre>' . $result . '</pre>';
Here is the beginning of $result
%PDF-1.4
1 0 obj
<<
Now, I would like the user to download this pdf file. I’ve used my client's old code because I didn’t write the web service (I've created the module and all the php code to connect with SoapClient
).
He was doing this :
header("Content-disposition: attachment; filename=bulletin.pdf");
header("Content-Type: application/pdf");
echo $result;
exit;
And here is my issue :
It’s working but I’ve noticed that there is some HTML added to the beginning of the pdf source code. Chrome, Firefox, Preview (OSX) can read the file but acrobat can’t. And I don’t want to let this extra HTML.
The extra HTML added is the current page HTML code (where there is the form).
Here is the PDF file source code (my-pdf.pdf) if I open it with a code editor :
<!DOCTYPE html>
<html lang="fr" dir="ltr">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
%PDF-1.4
1 0 obj
<<
How can I prevent this ? If I put all my php code in a different php file (outside Drupal without html, the code works).
I’ve tested a lot of things (different header, flush the buffer etc.) :
ob_clean();
flush();
header("Content-type:application/pdf");
header("Content-Disposition:attachment;filename='downloaded.pdf'");
echo $result;
exit;
what’s wrong with my header
and why I get this html code at the beginning ? Is there a way to generate this file without echo $result
;