3

I'm trying to get a page title to show up when outputting a pdf with fpdf.

Currently I only get the path to the .php file I'm using to create the pdf. e.g.

www.mydomain/inc/pdf/php

If I output anything other than the fpdf code e.g.

<title>This is my title</title>

or

<title><? echo $title ?></title>

I get an error like this...

Some data has already been output, can't send PDF

This is because you can only output fpdf data on the page apparently. So is it possible to add a title tag somehow with fpdf or rather in conjunction with fpdf?

It would just be much nicer to see a page title rather than the path to my pdf.php file.

Cheers

Bjorn
  • 285
  • 3
  • 6
  • 19

2 Answers2

5

FPDF sends a PDF file, not HTML. So by default, the browser just prompts the person to download it, or if you have a plugin, it will open it in the browser. As far as using a title, the PDF viewer or PDF plugin will most likely use the file name or title of the document as the title of the page. ($pdf->SetTitle('FPDF tutorial');)

Again, FPDF outputs PDF not HTML. If you start output-ing HTML, then try to echo the PDF content, it will be jibberish. PHP will also not let you modify headers after output (HTML) has started, which throws an error.

If you stored the PDF on the system or were able to configure routes to the FPDF module, you could use HTML and the <object> tag to include it on a page, but that would be a different set up and would require a few more steps. By doing this, you could add all the HTML you wanted, and just show the PDF in a small screen on the page.

Tim Withers
  • 12,072
  • 5
  • 43
  • 67
  • Unfortunately I've tried `$pdf->SetTitle` etc and in this instance it doesn't work, might look into the `` tag option. Thanks Tim :) – Bjorn Sep 05 '12 at 22:52
3

A pdf file is not an HTML file. The web browser shows a title because reads HTML code and parses it.

Sometimes the web browser renders PDF files because of an external plugin, so, if you want to render HTML code and a PDF file, you have to learn a bit of HTML and use frames or iframes.

Fast snippet:

<html>
  <header>
    <title>Heya, I'm not a path!</title>
  </header>
  <body>
    <iframe src="Put here the path of the web/file you want to show"></iframe>
  </body>
</html>

As you can see, there's an iframe tag, the iframe is like a box that loads another webpage inside of it, so the web browser will render the HTML code (title included), and then render a box with another different webpage inside (the PDF file), using css you can configure the iframe tag to adjust to web browser size.

Jorge Fuentes González
  • 11,568
  • 4
  • 44
  • 64
  • Cool Jorge, didn't think of an iframe, I don't use them often. I'll give the iframe a go and also Tim's suggestions above and see what happens. :) – Bjorn Sep 05 '12 at 22:53
  • iframe works! Just have to figure out an easy way to pass my post data to fpdf as now it has to go through 2 pages. Didn't really want to use sessions but may make things easier. – Bjorn Sep 05 '12 at 23:43
  • You can gather post data in the PHP file and send it vía GET to the iframe or autopost it just like Facebook does with canvas, here you can see their code: https://developers.facebook.com/docs/canvas/post/ scroll down to 'Solution' – Jorge Fuentes González Sep 05 '12 at 23:50
  • Ahh nice one, I'll check it out :) – Bjorn Sep 05 '12 at 23:51
  • GET almost worked it seems to have a problem with a large portion of text, I might just retrieve from the database, should have done that in the first place :P – Bjorn Sep 06 '12 at 00:28