How to get page title on fpdf output?

Bjorn picture Bjorn · Sep 6, 2012 · Viewed 7.1k times · Source

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

Answer

Tim Withers picture Tim Withers · Sep 6, 2012

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.