AlbertEngelB
AlbertEngelB

Reputation: 16456

Viewing PDF Inside Browser - Broken in Chrome Using POST

So I don't know when, but apparently there's been a change with Chrome that has broken viewing PDF's inside the browser. Viewing these inside Firefox or IE work; this seems to be a Chrome-only issue.

As of now, how I'm doing it is like this. Users POST data to the server, which does some magic with PDF population, and sends the PDF back to the browser. We're using the Content-Disposition, Content-Length, and Content-Type headers (I'm assuming) correctly:

Content-Disposition: "inline; filename=\"some-pdf.pdf\""
Content-Length: "1860799"
Content-Type: "application/pdf"

Is there any specific reason why Chrome refuses to load these files using the in-browser PDF viewer?

EDIT:

It seems as though Chrome doesn't load inline files when POSTing to a server.

Working Example:

var express = require('express');
var fs = require('fs');

var app = express();

app.listen(3000, function() {
  console.log('Listening on http://localhost:3000');
});

app.get('/', function(req, res, next) {
  fs.stat('./some-pdf.pdf', function(e, stats) {
    if(e) throw e;

    var stream = fs.createReadStream( './some-pdf.pdf' );

    res.setHeader('Content-disposition', 'inline; filename="some-pdf.pdf"');
    res.setHeader('Content-Length', stats.size);
    res.setHeader('Content-type', 'application/pdf');

    stream.pipe(res);
  });
});

Non-Working Example:

var express = require('express');
var fs = require('fs');

var app = express();

app.listen(3000, function() {
  console.log('Listening on http://localhost:3000');
});

app.get('/', function(req, res, next) {
  res.send('<form method="POST"><input type="submit", value="Get PDF"></form>');
});

app.post('/', function(req, res, next) {
  fs.stat('./some-pdf.pdf', function(e, stats) {
    if(e) throw e;

    var stream = fs.createReadStream( './some-pdf.pdf' );

    res.setHeader('Content-disposition', 'inline; filename="some-pdf.pdf"');
    res.setHeader('Content-Length', stats.size);
    res.setHeader('Content-type', 'application/pdf');

    stream.pipe(res);
  });
})

Upvotes: 1

Views: 1894

Answers (1)

Adriano
Adriano

Reputation: 64

It's a Chrome PDF Viewer bug. When you try to save the PDF, sometimes the plug-in re-request the page to be saved. If it's a HTTP Post, there is an internal error and the save fails.

It can be seen here: https://code.google.com/p/chromium/issues/detail?id=224520

Upvotes: 1

Related Questions