Opening a New Tab to Read a PDF File

How to Open .pdf on a new Tab

Solved! That works for me

 window.open('/Export/PrintPdf');

Opening PDF file in new tab angular 4?

From @barbsan idea, I changed the http headers and received a blob and used that to display the blob as pdf using window.open(). It worked.

Here is my sample code.

In service file

downloadPDF(url): any {
const options = { responseType: ResponseContentType.Blob };
return this.http.get(url, options).map(
(res) => {
return new Blob([res.blob()], { type: 'application/pdf' });
});
}

In component file

this.dataService.downloadPDF(url).subscribe(res => {
const fileURL = URL.createObjectURL(res);
window.open(fileURL, '_blank');
});

Open link in new tab of browser from PDF file

Short answer: It is not possible in a cross-plattform, guaranteed-to-work way.

Long answer: Hyperlinks in a PDF are different from Hyperlinks in HTML. PDF was not designed to be viewed as part of a browsing experience. Hence there is no option available for PDF Hyperlinks to open them in a new tab, because PDF does not know about the concept of tabs.

There is some discussion in Adobe's forums about it, which boils down to „not directly possible, but you could embed JavaScript in the PDF to do it“. They give an EPS file as example:

%!PS-Adobe-3.0 EPSF-3.0
%%BoundingBox: 0 0 100 100
%%EndProlog

[ /Rect [ 0 0 100 100 ]
/Action << /Subtype /JavaScript /JS (app.launchURL\("PLACE-YOUR-URL-HERE", true\);) >>
/Subtype /Link

/ANN pdfmark

%%EOF

Now before you try and get this EPS file embedded in your PDF, be aware that Chrome's PDF viewer has very little support for embedded JavaScript, so it is not guaranteed to work. It may also issue a warning to the User that there is JavaScript code going to be executed if they click on it. I would say it isn't worth the hassle.

Open pdf files in new tab

I think your missing a few things (like the file extension):

//no extension
$file = 'view';

//this is literally $file and never used again
$filename = '$file';

header('Content-type: application/pdf');
//the name of the transfered file is filename.php (not really an issue in this case)
//but is that what you wanted there
header('Content-Disposition: inline; filename="filename.pdf"');
header('Content-Transfer-Encoding: binary');
//still no extension on 'view' so your file is just named view (nothing else)
//no file path either.
header('Content-Length: ' . filesize($file));
header('Accept-Ranges: bytes');
//and you guessed it still no extension (error suppression hides the file not found error)
@readfile($file);

The $filename is $file literally not the value of the variable because of the use of single quotes. PHP does not do variable interpolation (value replacement) on single quoted strings. Not that it matters because it's never used again.

The @ probably shouldn't be there or you would see failed to find the file named view with not extension. You should be seeing some stuff like this

Warning: filesize(): stat failed for view in C:\Unit\eval\1541064182.php on line 2

Warning: readfile(view): failed to open stream: No such file or directory in C:\Unit\eval\1541064182.php on line 3

Except it will have your path and not my path to my Unit testing server.

In other words your readfile call is literately this:

 @readfile('view');

You can leave the download name as filename.php but I don't think that is the intention.

$file = 'view.pdf'; 

$pathname = $path.$file;

header('Content-type: application/pdf');
header('Content-Disposition: inline; filename="'.$file.'"');
header('Content-Transfer-Encoding: binary');
header('Content-Length: ' . filesize($pathname));
header('Accept-Ranges: bytes');

readfile($pathname);

Obviously you'll have to put in the value for the path, or if its in the same folder then you can ignore that.

Oh and last thing get rid of this thing ?> because if you have a line return after that, it may corrupt your file. I forget if that (specifically just a line return) is an issue for PDF, but any content after the closing tag (or before) will become part of the file data. The end tag is optional and so why risk it.



Related Topics



Leave a reply



Submit