Naib Sorion
Naib Sorion

Reputation: 504

PHP : How to change page title of PDF in browser

I have codes here to view pdf file in browser but I can't find how to change the page title and the title bar. Is this possible to customize title?

$file = "additionalInfo_content/QXp6dk3ZB1.pdf";
$fp = fopen($file, "r") ;
$myFileName = 'test';
header("Cache-Control: maxage=1");
header("Pragma: public");
header("Content-type: application/pdf");
header("Content-Disposition: inline; filename=".$myFileName."");
header("Content-Description: PHP Generated Data");
header("Content-Transfer-Encoding: binary");
header('Content-Length:' . filesize($file));
ob_clean();
flush();
while (!feof($fp)) {
   $buff = fread($fp, 1024);
   print $buff;
}
exit;

The page title show's what the last string in my URL which is the QXp6dk3ZB1.pdf as well the title bar. I want to change it on what my desire title

Upvotes: 3

Views: 7447

Answers (2)

Nicholas Long
Nicholas Long

Reputation: 31

You can control the window title that displays when loading a pdf through php by using your htaccess file Rewrite rules to hide the php processing file. Example using the htaccess RewriteRule ^uploads/([^/]+)/([^/]+)/([^/]+)(/)?$ /file.php?source=$1&file=$2&name=$3 [NC,L,QSA]

  • If your url were "https://example.com/uploads/files/QXp6dk3ZB1/myfilename.pdf", then you can pull your file QXp6dk3ZB1.pdf but have the clean filename "myfilename.pdf" show in the browser window.

Upvotes: 1

Capsule
Capsule

Reputation: 6159

The title is normally changed using the HTML <title> element. When viewing a file that is not HTML, it just displays the filename, or the internal PDF meta-title, if available.

You would have to alter the meta data of your file before sending it to the browser. Among other methods, there's this one: http://php.net/manual/en/function.pdf-set-info.php

Upvotes: 2

Related Questions