title: How to create and download a file with pdf source code stored in a variable?
body: <p>I’ve built a webservice in a Drupal Module. My issue is not related to Drupal or webservice. It works fine.</p>
  
  <p>User fills a form, submits and is connected to the webservice.</p>
  
  <p>When user is connected to this webservice, I get a PDF source code that I store in a variable : <code>$result</code></p>
  
  <p>If I display the content of <code>$result</code> for testing purpose, <strong>the PDF source code is correct</strong>.</p>
  
  <pre><code>echo '&lt;pre&gt;' . $result . '&lt;/pre&gt;';
  </code></pre>
  
  <p>Here is the beginning of <code>$result</code></p>
  
  <pre><code>%PDF-1.4
  1 0 obj
  &lt;&lt;
  </code></pre>
  
  <p>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 <code>SoapClient</code>).</p>
  
  <p>He was doing this :</p>
  
  <pre><code>header("Content-disposition: attachment; filename=bulletin.pdf");
  header("Content-Type: application/pdf");
  echo $result;
  exit;
  </code></pre>
  
  <p><strong>And here is my issue</strong> :</p>
  
  <p>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.</p>
  
  <p>The extra HTML added is the current page HTML code (where there is the form).</p>
  
  <p>Here is the PDF file source code (my-pdf.pdf) if I open it with a code editor :</p>
  
  <pre><code>&lt;!DOCTYPE html&gt;
  &lt;html lang="fr" dir="ltr"&gt;
  &lt;head&gt;
  &lt;meta http-equiv="Content-Type" content="text/html; charset=UTF-8"&gt;
  &lt;meta name="viewport" content="width=device-width, initial-scale=1"&gt;
  
  %PDF-1.4
  1 0 obj
  &lt;&lt;
  </code></pre>
  
  <p>How can I prevent this ? If I put all my php code in a different php file (outside Drupal without html, the code works).</p>
  
  <p>I’ve tested a lot of things (different header, flush the buffer etc.)  :</p>
  
  <pre><code>ob_clean();
  flush();
  header("Content-type:application/pdf");
  header("Content-Disposition:attachment;filename='downloaded.pdf'");          
  echo $result;
  exit;
  </code></pre>
  
  <p>what’s wrong with my <code>header</code> and why I get this html code at the beginning ? Is there a way to generate this file without <code>echo $result</code>;</p>