1

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;

Lajos Arpad
  • 64,414
  • 37
  • 100
  • 175
Sébastien Gicquel
  • 4,227
  • 7
  • 54
  • 84

2 Answers2

1

Frameworks are great tools which help you in generating common things into your pages. In this case it is highly probable that your framework is causing the extra HTML. You can test this by creating a php file without any frameworks, which gives you the file. POST to it and if you do not have an HTML, then my opinion is confirmed. In this case you will need to achieve this in Drupal.

Lajos Arpad
  • 64,414
  • 37
  • 100
  • 175
  • Yes, you’re right, as I mentioned in my question, I’ve tested all my PHP code in a file outside Drupal (a file without HTML, only PHP) and it is working fine. I’ve read before asking a lot of question on SO and it seems that `echo $results` is the right way to generate the file after sending appropriate `http-headers'. – Sébastien Gicquel Nov 10 '17 at 11:25
  • I’ve found a solution. I’ve posted my answer. Not sure it is a good approach but the PDF source code has no HTML code in it. What do you think of my solution ? – Sébastien Gicquel Nov 10 '17 at 12:56
  • 1
    @SébastienGicquel it should work, but rather than removing the header which was already written, it is better to writing unnecessary headers in the first place. You could use AJAX for example: https://api.drupal.org/api/examples/ajax_example%21ajax_example.module/group/ajax_example/7.x-1.x – Lajos Arpad Nov 10 '17 at 13:00
  • 1
    @SébastienGicquel Or you could use Javascript to download the file, which would keep your server load reduced, as the file will be downloaded directly from the client-side: https://stackoverflow.com/questions/12676649/javascript-programmatically-trigger-file-download-in-firefox – Lajos Arpad Nov 10 '17 at 13:01
0

I’ve found a solution but I don’t know if it is the best approach. The idea is to clean the extra html added in the pdf source code. I use ob_end_clean

ob_end_clean — Clean (erase) the output buffer and turn off output buffering

Final code that works :

ob_end_clean();
header("Content-type:application/pdf");
header("Content-Disposition:attachment;filename='downloaded.pdf'");
ob_end_clean();
print $result;
exit();

Note : I don’t know why but iI had to clean before and after the http-headers

Sébastien Gicquel
  • 4,227
  • 7
  • 54
  • 84