Rectangle 27 244

To indicate to the browser that the file should be viewed in the browser:

To have the file downloaded rather than viewed:

Content-Type: application/pdf
Content-Disposition: attachment; filename="filename.pdf"

EDIT: The quotes around the filename are required if the filename contains special characters such as filename[1].pdf which may otherwise break the browser's ability to handle the response.

@PapickG.Taboada but then the user's system may not know the file type. E.g. some user's may have opted to "Always open files of this type" for PDF files. Perhaps if you want to override the user's preferences then octet-stream would be the way to go, but giving the correct type and a suggested filename is the "correct" way to provide a download.

hi @ColinM I am a bit confused here... we are having issues rendering the pdf, it just gives a scrambled text. where do we set the Content-Type: application/pdf Content-Disposition: inline; "filename.pdf"? 'cos, we upload it using angular-js code. So my question is should the content type be set before uploading? And also, we get only a link from the backend team, a url that gives the file path, that we open in new tab using:window.open(url, '_blank').focus();

@Kailas I don't understand what you're trying to do.. The answer is referring to the headers that a server should send to a client when responding to an HTTP request for the PDF file. These headers have no effect on a file upload, you need to have the code behind the url set the headers every time it is downloaded by the client.

@ColinM Thanks buddy, you said it right, the issue when we debugged was the mime type was set while uploading the files. This should be done by the back-end team. I tried to get codes on how to add headers in java script but was not successful. Thanks, as I got the real idea cleared from you... :)

http headers - How to force files to open in browser instead of downlo...

pdf http-headers browser embed embedding
Rectangle 27 244

To indicate to the browser that the file should be viewed in the browser:

To have the file downloaded rather than viewed:

Content-Type: application/pdf
Content-Disposition: attachment; filename="filename.pdf"

EDIT: The quotes around the filename are required if the filename contains special characters such as filename[1].pdf which may otherwise break the browser's ability to handle the response.

@PapickG.Taboada but then the user's system may not know the file type. E.g. some user's may have opted to "Always open files of this type" for PDF files. Perhaps if you want to override the user's preferences then octet-stream would be the way to go, but giving the correct type and a suggested filename is the "correct" way to provide a download.

hi @ColinM I am a bit confused here... we are having issues rendering the pdf, it just gives a scrambled text. where do we set the Content-Type: application/pdf Content-Disposition: inline; "filename.pdf"? 'cos, we upload it using angular-js code. So my question is should the content type be set before uploading? And also, we get only a link from the backend team, a url that gives the file path, that we open in new tab using:window.open(url, '_blank').focus();

@Kailas I don't understand what you're trying to do.. The answer is referring to the headers that a server should send to a client when responding to an HTTP request for the PDF file. These headers have no effect on a file upload, you need to have the code behind the url set the headers every time it is downloaded by the client.

@ColinM Thanks buddy, you said it right, the issue when we debugged was the mime type was set while uploading the files. This should be done by the back-end team. I tried to get codes on how to add headers in java script but was not successful. Thanks, as I got the real idea cleared from you... :)

http headers - How to force files to open in browser instead of downlo...

pdf http-headers browser embed embedding
Rectangle 27 4

If you want your pdf file to be downloadable by anyone who could browse your website just by clicking on links, then you can use the download attribute (for tags) which allows you to specify the linked resource :

Just remember that browser compatibility for the download attribute is not great yet: caniuse.com/#feat=download

php - Download pdf from href link issue - Stack Overflow

php wordpress pdf download
Rectangle 27 15

If you are using HTML5 (and i guess now a days everyone uses that), there is an attribute called download.

<a href="somepathto.pdf" download="filename">

here filename is optional, but if provided, it will take this name for downloaded file.

If you have control over the server code you should use 'attachement' as this will allow to use the same filename generation code. If you have no control over the server this is a good solution.

This is a brilliant solution to the problem however, as always, IE is holding us back from using it: caniuse.com/#search=download

Important to note that this does not work across domains (e.g., same-origin policy gets in the way). If downloading from one domain, the download attribute will not work if content is stored on a different domain. CORS may allow that content to pass through (haven't tested).

This is literally the opposite of what the OP is asking :)

http headers - How to force files to open in browser instead of downlo...

pdf http-headers browser embed embedding
Rectangle 27 15

If you are using HTML5 (and i guess now a days everyone uses that), there is an attribute called download.

<a href="somepathto.pdf" download="filename">

here filename is optional, but if provided, it will take this name for downloaded file.

If you have control over the server code you should use 'attachement' as this will allow to use the same filename generation code. If you have no control over the server this is a good solution.

This is a brilliant solution to the problem however, as always, IE is holding us back from using it: caniuse.com/#search=download

Important to note that this does not work across domains (e.g., same-origin policy gets in the way). If downloading from one domain, the download attribute will not work if content is stored on a different domain. CORS may allow that content to pass through (haven't tested).

This is literally the opposite of what the OP is asking :)

http headers - How to force files to open in browser instead of downlo...

pdf http-headers browser embed embedding
Rectangle 27 5

The correct type is application/pdf for PDF, not application/force-download, this looks like a hack for some legacy browsers. Always use the correct mimetype if you can.

If you have control over the server code:

header("Content-Disposition", "attachment; filename=myfilename.myextension");
header("Content-Disposition", "inline; filename=myfilename.myextension");

No control over the server code:

  • Use the HTML5 download attribute, uses custom filename specified on the view side.

NOTE: I prefer setting the filename on the server side as you may have more information and can use common code.

http headers - How to force files to open in browser instead of downlo...

pdf http-headers browser embed embedding
Rectangle 27 5

The correct type is application/pdf for PDF, not application/force-download, this looks like a hack for some legacy browsers. Always use the correct mimetype if you can.

If you have control over the server code:

header("Content-Disposition", "attachment; filename=myfilename.myextension");
header("Content-Disposition", "inline; filename=myfilename.myextension");

No control over the server code:

  • Use the HTML5 download attribute, uses custom filename specified on the view side.

NOTE: I prefer setting the filename on the server side as you may have more information and can use common code.

http headers - How to force files to open in browser instead of downlo...

pdf http-headers browser embed embedding
Rectangle 27 47

I assume you want the PDF to display in the browser, rather than forcing a download. If that is the case, try setting the Content-Disposition header with a value of inline.

Also remember that this will also be affected by browser settings - some browsers may be configured to always download PDF files or open them in a different application (e.g. Adobe Reader)

Yes. But when I'm trying your suggestion, I get an error says "File does not begin with '%PDF-'". I use 'Content-Disposition: inline; filename="the.pdf"'. Do I made a mistake?

@dimassony maybe show the full code

sounds like it's not a valid PDF file?

good to hear, and great to know the solution! It'd be good if you could post the final code showing which headers you're sending, for anyone else who comes across this question.

Show a PDF files in users browser via PHP/Perl - Stack Overflow

php perl pdf
Rectangle 27 3

<FilesMatch "\.(?i:pdf)$">
    ForceType application/octet-stream
    Header set Content-Disposition attachment
</FilesMatch>

http headers - How to force files to open in browser instead of downlo...

pdf http-headers browser embed embedding
Rectangle 27 3

<FilesMatch "\.(?i:pdf)$">
    ForceType application/octet-stream
    Header set Content-Disposition attachment
</FilesMatch>

http headers - How to force files to open in browser instead of downlo...

pdf http-headers browser embed embedding
Rectangle 27 8

If you want to convert all the downloaded images into one pdf then execute

convert img{0..19}.jpg slides.pdf

this has the same problem as the example shown in the question

Do not use convert for postscript or PDF files unless you go from vector to raster and never go back. It is hard to overstate what a bad idea this is.

linux - Merge / convert multiple PDF files into one PDF - Stack Overfl...

linux pdf merge command-line-interface
Rectangle 27 7

If you want to convert all the downloaded images into one pdf then execute

convert img{0..19}.jpg slides.pdf

this has the same problem as the example shown in the question

Do not use convert for postscript or PDF files unless you go from vector to raster and never go back. It is hard to overstate what a bad idea this is.

linux - Merge / convert multiple PDF files into one PDF - Stack Overfl...

linux pdf merge command-line-interface
Rectangle 27 1

<?php
// place this code inside a php file and call it f.e. "download.php"
$path = $_SERVER['DOCUMENT_ROOT']."/path2file/"; // change the path to fit your websites document structure
$fullPath = $path.$_GET['download_file'];
if ($fd = fopen ($fullPath, "r")) {
    $fsize = filesize($fullPath);
    $path_parts = pathinfo($fullPath);
    $ext = strtolower($path_parts["extension"]);
    switch ($ext) {
        case "pdf":
        header("Content-type: application/pdf"); // add here more headers for diff. extensions
        header("Content-Disposition: attachment; filename=\"".$path_parts["basename"]."\""); // use 'attachment' to force a download
        break;
        default;
            header("Content-type: application/octet-stream");
            header("Content-Disposition: filename=\"".$path_parts["basename"]."\"");
    }
    header("Content-length: $fsize");
    header("Cache-control: private"); //use this to open files directly
    while(!feof($fd)) {
        $buffer = fread($fd, 2048);
        echo $buffer;
    }
}
fclose ($fd);
exit;
// example: place this kind of link into the document where the file download is offered:
// <a href="download.php?download_file=some_file.pdf">Download here</a>
?>

php - How to start download of file? - Stack Overflow

php file download
Rectangle 27 4

Needed to do this yesterday and here's how without any external non-PHP libs that need to be installed. The only 2 libs needed are both PHP libs and are easy to get.

Now you can use the class below to achieve watermarking

/** MAKE SURE TO HAVE THE INCLUDES RUNNING PROPERLY */
require_once('FPDF/fpdf.php');
require_once('FPDI/fpdi.php');

class WaterMark
{
    public $pdf, $file, $newFile,
        $wmText = "STACKOVERFLOW";

    /** $file and $newFile have to include the full path. */
    public function __construct($file, $newFile)
    {
        $this->pdf =& new FPDI();
        $this->file = $file;
        $this->newFile = $newFile;
    }

    /** $file and $newFile have to include the full path. */
    public static function applyAndSpit($file, $newFile)
    {
        $wm = new WaterMark($file, $newFile);

        if($wm->isWaterMarked())
            return $wm->spitWaterMarked();
        else{
            $wm->doWaterMark();
            return $wm->spitWaterMarked();
        }
    }

    /** @todo Make the text nicer and add to all pages */
    public function doWaterMark()
    {
        $currentFile = $this->file;
        $newFile = $this->newFile;

        $pagecount = $this->pdf->setSourceFile($currentFile);

        for($i = 1; $i <= $pagecount; $i++){
                            $this->pdf->addPage();
            $tplidx = $this->pdf->importPage($i);
            $this->pdf->useTemplate($tplidx, 10, 10, 100);
            // now write some text above the imported page
            $this->pdf->SetFont('Arial', 'I', 40);
            $this->pdf->SetTextColor(255,0,0);
            $this->pdf->SetXY(25, 135);
            $this->_rotate(55);
            $this->pdf->Write(0, $this->wmText);
                            $this->_rotate(0);
        }

        $this->pdf->Output($newFile, 'F');
    }

    public function isWaterMarked()
    {
        return (file_exists($this->newFile));
    }

    public function spitWaterMarked()
    {
        return readfile($this->newFile);
    }

    protected function _rotate($angle,$x=-1,$y=-1) {

        if($x==-1)
            $x=$this->pdf->x;
        if($y==-1)
            $y=$this->pdf->y;
        if($this->pdf->angle!=0)
            $this->pdf->_out('Q');
        $this->pdf->angle=$angle;

        if($angle!=0){
            $angle*=M_PI/180;
            $c=cos($angle);
            $s=sin($angle);
            $cx=$x*$this->pdf->k;
            $cy=($this->pdf->h-$y)*$this->pdf->k;

            $this->pdf->_out(sprintf(
                'q %.5f %.5f %.5f %.5f %.2f %.2f cm 1 0 0 1 %.2f %.2f cm',
                $c,$s,-$s,$c,$cx,$cy,-$cx,-$cy));
        }
    } 

}

You now can run this as:

WaterMark::applyAndSpit($fileWithFullPath);

I used this and was getting all the pages displaying on the first page. I was able to fix this by adding $this->pdf->addPage(); to the end of the loop in doWaterMark(). I made some other minor changes so I'm not confident to edit your answer but this may help if others have this problem.

Thanks for the code. A couple of little things I've found so far keeping this from being plug-n-play. The "run this" command needs two args: WaterMark::applyAndSpit($fileWithFullPath,$newfilenameWithFullPath); ,and the class uses =&, which is deprecated in current php, just use = .

php - Applying watermarks on pdf files when users try to download the ...

php pdf pdf-generation watermark
Rectangle 27 4

Although there are several very good PDF libs for PHP, if I were writing such a program I'd just shell out to run pdftk but you' still need to generate your watermark.

$tempfile=tempnam();
system("pdftk input_file.pdf background watermark.pdf output $tempfile dont_ask", $errcode);
if (!$errcode && $ih=fopen($tempfile, 'r')) {
    header('Content-Type: application/pdf');
    fpassthru($ih);
    fclose($ih);
} else {
    print "Whoops";
}
unlink($tempfile);

php - Applying watermarks on pdf files when users try to download the ...

php pdf pdf-generation watermark
Rectangle 27 1

header("Content-Type: application/force-download");
    header("Content-type: application/pdf");
    header("Content-Disposition: inline; filename=\"".$name."\";");

If you don't want the browser to prompt the user then use "inline" for the third string instead of "attachment". Inline works very well. The PDF display immediately without asking the user to click on Open. I've used "attachment" and this will prompt the user for Open, Save. I've tried to change the browser setting nut it doesn't prevent the prompt.

http headers - How to force files to open in browser instead of downlo...

pdf http-headers browser embed embedding
Rectangle 27 1

header("Content-Type: application/force-download");
    header("Content-type: application/pdf");
    header("Content-Disposition: inline; filename=\"".$name."\";");

If you don't want the browser to prompt the user then use "inline" for the third string instead of "attachment". Inline works very well. The PDF display immediately without asking the user to click on Open. I've used "attachment" and this will prompt the user for Open, Save. I've tried to change the browser setting nut it doesn't prevent the prompt.

http headers - How to force files to open in browser instead of downlo...

pdf http-headers browser embed embedding
Rectangle 27 4

You could modify a PDF renderer such as xpdf or evince to render into a graphics image on your server, and then deliver the image to the user. This is how Google's quick view of PDF files works, they render it locally, then deliver images to the user. No downloaded PDF file, and the source is pretty well obscured. :)

This is the only really safe way to prevent the theft of the original document.

Besides, it would not be theft, because the original is still there.

Here is a pure Java PDF renderer if you could server it pdf-renderer.dev.java.net

Show a PDF files in users browser via PHP/Perl - Stack Overflow

php perl pdf
Rectangle 27 2

There is an excellent opensource php library http://www.tcpdf.org/ , I use it for all pdf generating tasks.

php - Applying watermarks on pdf files when users try to download the ...

php pdf pdf-generation watermark
Rectangle 27 1

You mean a default image for pdf, doc and xls, not a preview? So you probably want something like:

<%= link_to(image_tag(preview_or_default_image(media)), media %>

and then define a helper:

def preview_or_default_image(media)
  case media.type
  when 'jpg', 'jpeg' # , 'png', ...
    media.image_url
  when 'doc', 'docx'
    'path/to/doc/default_image.png'
  when 'xls', 'xlsx'
    'path/to/xls/default_image.png'
  when 'pdf'
    'path/to/pdf/default_image.png'
  end
end
media
model.uploader.file.extension

Media is the carrierwave object, refer to question above ;)

ruby on rails 3 - How to set default image to download uploaded doc, p...

ruby-on-rails-3 image-processing carrierwave