Thursday, February 19, 2009

Displaying PDFs Via HTTP

Q: What additional software do I need in order to display PDFs via SQL Anywhere's built-in HTTP server?

A: Nothing, you've got it all: SQL Anywhere on the server side and Adobe Reader on the client side.



Here's the code for a web service written in "Hello World" style... it really is this simple:

CREATE SERVICE root 
TYPE 'RAW' AUTHORIZATION OFF USER DBA
AS CALL p();

CREATE PROCEDURE p()
RESULT ( html_string LONG VARCHAR )
BEGIN

CALL dbo.sa_set_http_header( 'Content-Type', 'application/pdf' );

SELECT xp_read_file ( 'This is Robot Chicken.pdf' );

END;
Here's how to start the SQL Anywhere engine with the HTTP server enabled:
   "%SQLANY11%\bin32\dbsrv11.exe" -xs http ddd11.db
Here's the URL:
   http://localhost/
Some notes:
  • The key is specifying the 'Content-Type' as 'application/pdf'.

  • Other content-types are possible, such as 'image/jpg'; see MIME Reference (which, curiously, does not mention 'image/png').

  • The point about 'image/png' may be moot; see HTTP content-type and browser support ...which might help explain Safari's (lack of) popularity :)

  • The service name "root" is special; it means you don't have to code it in the URL, although http://localhost/root still works.

  • In the real world, you might not read the file from disk via xp_read_file(), you might store the PDF in a LONG BINARY database column and select that. That way ALL your data is protected by SQL Anywhere.

1 comment:

Anonymous said...

Just an addition:

W.r.t. setting up the content-type easily, Eric Farrar's blog my give good advice:

http://iablog.sybase.com/efarrar/2008/10/setting-the-mime-type

Regards
Volker