0

Users upload PDF files using my PHP application and I store them on S3. At some later point other users view the files, which I display 'inline' in their browser.

The problem is that the 'Title' attribute of the PDF is displayed in teh browser tab where the web site title would normally be displayed. As it is set by the user who did the original upload, it is arbitrary, and I therefore need to change it. How do I do this?

I thought that Title was an extended attribute of the file, however installed Ubuntu's xattr, and when I run it on the file, it returns nothing, so perhaps I am mistaken.

When I view the object metadata on S3, there is no mention of a Title attribute, so I don't know where/how it is stored on S3.

My preference would be to rewrite the Title attirbute using an OS call, rather than installing another PHP extension (such as xattr).

EDIT: Here is the Laravel controller method which returns the response

public function displayFile($id)
{
    $headers = ['Content-Type' => 'application/pdf', 'Content-Disposition' => 'inline'];
    return response(Storage::disk('private')->get("files/{$id}.pdf"), 200, $headers);
}
DatsunBing
  • 8,684
  • 17
  • 87
  • 172
  • I wouldn't rewrite the 'Title' attribute of the documents; I'd leave them alone and set the title for the browser tab in your code HTML, for example: `Whatever Title You Want...` Can you edit to show your PHP code? – B. Shefter Mar 18 '19 at 20:59
  • Is it always about a PDF file? What about this: https://stackoverflow.com/questions/2740297/display-adobe-pdf-inside-a-div – Nico Haase Mar 18 '19 at 21:08
  • 1
    @B.Shefter, as you can see, there is no html surrounding the content. – DatsunBing Mar 18 '19 at 21:11

3 Answers3

1

When you say 'inline' what exactly do you mean? What you are describing seems more like you are pointing them to the document url. In this case, the title will be the one contained in the PDF, which only some PDF editor could change. I know of none that will not break files (especially ones with interactive content) BADLY for PHP. If you have access to native apps, you can try using exiftool: https://askubuntu.com/questions/27381/how-to-edit-pdf-metadata-from-command-line

What you might want to do is actually display the document inside a HTML file, like this: https://www.w3docs.com/snippets/html/how-to-embed-pdf-in-html.html Note: do extensive testing for various browsers, especially mobile; PDF embedding is notoriously quirky in mobile browsers.

Dinu
  • 1,374
  • 8
  • 21
1

You should be able to add an arbitrary filename to Content-Disposition for inline viewing, just as you could if you were downloading. Try something like this:

$headers = ['Content-Type' => 'application/pdf', 'Content-Disposition' => 'inline; filename=\"WhateverYouWantTheUsersToSee\"'];

(Don't actually know whether you need to escape those quotation marks; if not, take out the backslashes.)

B. Shefter
  • 877
  • 7
  • 19
  • The Content-Disposition filename is not a title. The title is a property of the PDF data itself. –  Mar 18 '19 at 21:25
  • Thanks for the suggestion, but it did not work - either with/without quotation marks – DatsunBing Mar 18 '19 at 21:29
  • @Kim Prince Oh, you definitely need quotation marks; I just wasn't sure you needed to escape them (i.e., precede them with backslashes). You probably don't. Editing to clarify. – B. Shefter Mar 18 '19 at 21:34
  • @duskwuff Yes, but you don't have to use the PDF's original title when downloading; you can save to any other filename you specify. Any reason the same principle wouldn't work for inline viewing, that is, display it with a new filename (shown in the browser tab) without changing the original PDF in any way? – B. Shefter Mar 18 '19 at 21:36
  • 2
    @B.Shefter I think you're missing my point. The title of a PDF is a completely separate property from its filename, just like the title of an HTML document is separate from its filename. –  Mar 18 '19 at 23:24
  • 1
    this solution not work. see my answer for the solution – Francesco Taioli Apr 06 '20 at 11:19
0

The problem is that to set the title, the title must set into the pdf, but there is a workaround ( see explanation )

Explanation:

The page with target "_blank" attribute, set the file names base on the last part of the url. So if the url is my.site/32/55 , the html title is 55.

The workaround is to set the file name as the last part of the url

1) Web.php

This is the most important part. To give the page the pdf title, set the title you want as the last param

Route::get('/pdfSee/{fileID}/{htmlTitle}', 'FileController@viewInTheBrowser')->name('file.see');

2) Controller

public function viewInTheBrowser(File $file){
$basePath = 'public/myFolder/'; 
        return  response()->download(storage_path("app/".$this->basePath. $file->file_system_path), $file->file_name, [], 'inline');
    }

3) View

<a href="{{ URL::route('file.see', [$file, $file->file_name]) }}" target="_blank"> Download </a>

As you can see, we pass in the route file.see the actual file name as last param, so wen the html page is open, it takes the last param ( the file name ) as html page title

Francesco Taioli
  • 2,687
  • 1
  • 19
  • 34