Welcome! Log In Create A New Profile


Serving PDF files from MySQL

Posted by nightbeacons 
Serving PDF files from MySQL
January 27, 2020 03:59PM
We have a large number of PDF files that are stored as LONGBLOBs in a MySQL database.
We've written a Drupal-based PHP module that pulls the LONGBLOB data and formats it as a PDF download from the web page.

The PHP module adds the Content-Type: application/pdf and Content-Length: and Accept-Ranges: bytes headers (via php "header()" function) and then echoes the LONGBLOB data. (The headers and LONGBLOB are encapsulated in ob_start() / ob_end_flush() )

On the Nginx side, the relevant part of the config file looks like this:

# location ~ ^/support/userdoc/.*\.pdf {
location /support/userdoc/ {
default_type application/pdf;
add_header Content-Type 'application/pdf';
add_header Last-Modified $date_gmt;
if_modified_since off;
etag off;

location ~* \.(?:js|css|html|xml|png|jpg|mp4|pac|pdf|obj|mtl|doc|docx|jpeg|svg|gif|ico|gz|zip|crt|xlsm|ttf|vtt|eot|potx|woff|woff2)$ {
expires max;
add_header Cache-Control 'public, max-age=31536000, immutable';
# Below header allows server to display ttf/eot/woff/woff2 fonts
add_header Access-Control-Allow-Origin *;
log_not_found off;
access_log off;
tcp_nodelay on;
keepalive_timeout 65;
gzip_static on;
try_files $uri $uri/ @rewrite;

The "most specific" match seems to be the first one. Because no actual files are being sent, I've turned off etag.

The problem is that the LONGBLOB is sometimes sent as text/html (incorrect) and sometimes as application/pdf (correct).
And the Content-Length value is never sent.

(Obviously, I'd like to have the Content-type set to application/pdf for all of the LONGBLOB-based downloads)

The page in question is at https://www.sonosite.com/support/user-documents

Any suggestions would be most appreciated! Thank you.
Re: Serving PDF files from MySQL
January 28, 2020 01:33PM
An additional note:

The location instruction:

location /support/userdoc/ {

refers to a virtual directory. (It does not really exist in the file system, but is solely used as an entry point to the download)

However, the Nginx access log indicates that the directory is being handled appropriately:

[05/Jan/2020:11:11:15 -0800] "GET /support/userdoc/SII_Errata_14_Lgs_P22338-04A_e.pdf HTTP/1.1" 200 1589455 "-" "Mozilla/5.0 (Linux; Android 6.0.1;
Sorry, only registered users may post in this forum.

Click here to login

Online Users

Guests: 73
Record Number of Users: 6 on February 13, 2018
Record Number of Guests: 421 on December 02, 2018
Powered by nginx      Powered by FreeBSD      PHP Powered      Powered by MariaDB      ipv6 ready