I want to show a PDF content in a View with html tag as <object data="URL of PDF">.
My PDF content is saved in a Blob field, not as a file. So the URL source of data is not referred to a PDF file.
Then I modified URL as a link to an Controller action in View file as this:
View:
<?php
$base_url = "h*tp://". $_SERVER['SERVER_NAME'] . dirname($_SERVER["REQUEST_URI"].'?') ;
echo '<object style="width:100%;height:500px" data="'. $base_url.'/PDFget/id/'.$id . '" type="application/pdf">';
echo '</object>';
?>
I wrote an action in controller to read Blob field and echo the PDF content.
Controller:
public function actionPDFget($id)
{
$model=$this->loadmodel($id);
header('Content-type: application/pdf');
header('Cache-Control: public, must-revalidate, max-age=0');
header('Pragma: public');
header('Expires: 0');
header('Content-Transfer-Encoding: binary');
header('Content-length: '.$model->PDF_FileSize);
header('Content-Disposition: attachment; filename='.$model->PDF_FileName);
echo $model->PDF_Content;
}
But when running the code, in view no PDF is shown. I reviewed the page source and found the <object …> tag is correct:
<html>
<head>
...
...
</head>
<body>
...
...
<object style="width:100%;height:500px" data="h*tp://127.0.0.1/myweb/report/PDFget/id/18" type="application/pdf"></object>
...
...
</body>
</html>
If I paste the link h*tp://127.0.0.1/myweb/report/PDFget/id/18 in Browser address bar, it gets the PDF content as a file download.
In normal <object data="URL"> tag definition, the data URL most have a file link, but as a security matter I do not want to have PDF in a file and so send it directly from database to view as data of <object…> tag.
May I ask you any help to solve the problem, please.