Downloading PDF file with a servlet

I'm working on a web application that dynamicaly creates a PDF file and then writes each character to the ServletOutputStream. Ideally, the browser is supposed to recognize the "application/pdf" MIME type and force the file to be downloaded (or opened from current location if the user should choose). When I test it on my local system, the file downloads but it uses the servlet as the file name. On my webserver I get the following problems.
1) On all the browsers I've tested (except Internet Explorer), the output is being printed in ASCII to the display which keeps the file from downloading. Internet Explorer is the only browser that will actually download the file.
2) When it does attempt to download the file, it is creating a file with the servlet as the name as opposed to the filename that I am assigning during creation.
I've been reading all the related posts that I could find in these Forums but haven't been able to find a solution. If anybody has any ideas, I would greatly appreciate your knowledge. Please note that I have set up MIME-mapping for the PDF extension in the WEB.XML file on my web server.
Here is some servlet code that handles the PDF creation and download:
public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
InputStream in = null;
ServletOutputStream out = null;
String dir = "/www/mydirectory/";
try {
// Create the PDF file and retrieve the filename
String filename = PDFCreator.main();
//Flush the response stream and then reset it for writing a PDF to the display
response.reset();
response.setContentType("application/pdf");
response.setHeader("Content-Disposition", "attachment; filename="+filename);
out = response.getOutputStream();
in = new BufferedInputStream( new FileInputStream(dir+filename));
int ch;
while ( (ch = in.read()) != -1 ) {
out.print( (char)ch );
} catch (Exception e) {
e.printStackTrace();
} finally {
if( in != null ) {
in.close();
if( out != null ) {
out.flush();
out.close();

Your code looks correct.
Apparently other people too have been having problems with pdf's. These links may help you solve the problem.
http://forum.java.sun.com/thread.jsp?forum=54&thread=100793
http://forum.java.sun.com/thread.jsp?forum=33&thread=250668
http://forum.java.sun.com/thread.jsp?forum=33&thread=63282
Redirecting may not be a good idea due to url hacking. Streaming the output would be the best way to go.

Similar Messages

  • HT5701 Cannot download pdf files with Safari 6.0.4?

    Cannot download pdf files with Safari 6.0.4

    Back up all data.
    Triple-click the line of text below to select it, the copy the selected text to the Clipboard (command-C):
    /Library/Internet Plug-ins
    In the Finder, select
    Go ▹ Go to Folder
    from the menu bar, or press the key combination shift-command-G. Paste into the text box that opens (command-V), then press return.
    From the folder that opens, remove any items that have the letters “PDF” in the name. You may be prompted for your login password. Then quit and relaunch Safari, and test.
    The "Silverlight" web plugin distributed by Microsoft can also interfere with PDF display in Safari, so you may need to remove it as well, if it's present.
    If you still have the issue, repeat with this line:
    ~/Library/Internet Plug-ins
    If you don’t like the results of this procedure, restore the items from the backup you made before you started. Relaunch Safari again.

  • Cannot download pdf files with adobe

    I still have the same problem I had a month ago I cannot download pdf file from internet sites screen just goes black
    I have recently upgraded software to lion I have the latest adobe software installed still no joy.
    Was never a problem in the past on my IMac

    In the Finder, select Go ▹ Go to Folder... from the menu bar, or press the key combination shift-command-G. Copy the line of text below into the box that opens, and press return:
    /Library/Internet Plug-ins/AdobePDFViewer.plugin
    If a Finder window opens with that item selected, delete it, then quit and relaunch Safari. Otherwise post again.

  • HT1750 unable to download pdf files with the new lion instillation

    I am unable to download pdf files after installing lion.

    I have an imac purchased 4/2010 and same thing happened to me but can't find anyone to address it. The real tragedy here is that I paid for the full adobe creative suite, web premium back in 2010 and Adobe Pro can't open them, although it worked just fine before. Then I installed the latest version of Adobe reader and tried changing that to my default and that doesn't work either.
    It's pretty lame that I have to go on my boyfriends windows pc to open and print a .pdf as the only alternative. I did not read about this when researching whether or not to upgrade to lion from snow leopard otherwise I would not have kept the old operating system.
    Why isn't apple addressing this???

  • Downloading pdf files with firefox 6.0.2 doesn't always work but pdf files do download with IE

    for example the home page for the web site kerrville.org has several items to download and most are pdf files using firefox version 6.0.2 the download seems to start but never completes . If I use IE version 8 at the same web site I'm able to download any and all of the pdf files. I have tried this several times and get the same results each time.

    I would just get rid of the create PDF 1.1 and go with CutePDF instead. It's a much better program, it's free, and it doesn't create all the issues that the Adobe software does.

  • Can you sell automatic download pdf files with MUSE?

    I have a web site where I sell pdf files. I am currently using shopify but am not thrilled with their web deisgns.
    Is there a way (either through an app or widget) to sell pdf files? I would need the files to be immediate available for buyers upon check out.
    Also, I need to have their files available to them for a set time within their accounts. Is that possible?
    TIA for any help

    We use e-junkie, which hosts our files, $5/month. Great pop-up shopping cart. Download automatic, with pdf stamping.
    Seamless with Muse.
    But they don't offer page designs though.

  • Firefox won't download PDF files with javascript:doDownload();

    I just installed Firefox version 9.0.1 hoping it would solve this problem, but no such luck. My bank allows me to download and print my bank statements as PDF files from their website. I checked the code and the button I click on implements a javascript:doDownload(); function. A small panel opens that allows me to enter the page range. I then click OK and nothing happens. An earlier version of Firefox (3.0) had no problem with this and I was able to download and save my bank statements to my hardrive as PDF files.

    I found that the problem was with the addon PDF Download 3.0.0.2
    I disabled it and was able to download and print my bank statements. I'm just not sure if I need to change the settings or completely un-install it.

  • I cannot download .pdf files with the new version of Firefox

    Using Firefox 1.5, .pdf files automatically download to my desktop. With Firefox 3.6.13 a new tab/window opens and a box where the document should appear remains blank. When I attempt to 'right' click on the link, it doesn't allow me to download the linked file, only the linked address. I am able to open the .pdf files using another web browser such as Sarfari

    The Google Toolbar "will not be supported on Firefox 5 and future versions." <br />
    http://googletoolbarhelp.blogspot.com/2011/07/update-on-google-toolbar-for-firefox.html
    http://www.google.com/support/toolbar/bin/answer.py?answer=1342452&topic=15356%29
    The Google Toolbar '''7.1.20110512W''' version does work pretty good in Firefox 5.0 by using the Compatibility Reporter extension. But not every feature works 100%.<br />
    https://addons.mozilla.org/en-US/firefox/addon/add-on-compatibility-reporter/
    http://googlesystem.blogspot.com/2011/06/enable-google-toolbar-in-firefox-5.html

  • Cant download pdf files with long file names??

    Why in windows mobile wont it let me open/download pdf links th have long file names...works fine on desktop....just a mobile issue??? Have Lumia 930...windows mob 8.1

    Personally, I've not heard of this but maybe WP doesn't handle long names as well as Windows - what actually happens when you try? Is there an error message or does the download just fail silently?

  • Can't download PDF files with Safari

    When I try to download a PDF using Safari, it will save to my Desktop, but when I go to open it, I get the error "There was an error opening this document. The file is damaged and cannot be repaired." If I try to use the Print command and save it as a PDF, I get nothing but a blank document.

    Patrick,
    The following article from TUAW may be of help: http://tinyurl.com/9tqn45d. It states that the problem may be caused by 'AdobePDFViewerNPAPI.plugin' in the Internet Plugins folder. Deleting that plugin seems to fix the problem for many people.
    I don't have this problem even though I have the  'AdobePDFViewerNPAPI.plugin' installed. Perhaps the reason I don't get the error message is because I use the Adobe PDF Viewer as my default plugin for viewing PDFs in Safari. In fact, if I remove the 'AdobePDFViewerNPAPI.plugin', I can no longer see  PDFs in Safari, only download them.
    So, the error messages may exist only for users of Safari's built-in PDF-viewer. I find the Adobe Plugin has a few more tools than the built-in one. Using that one may be an alternate solution to this problem.

  • I cannot download .pdf files with 3.6.13, but I can with Internet Explorer

    I just installed 3.6.13, but had problem with 3.6.12. The screen turns black when the download "starts" and Done appears at the bottom of the screen. Running the Windows Task Manager shows only a less than 1 box spike. Sometimes the screen then goes white seconds later; sometimes it remains black until the screen is "clicked" (possibly has to do with the size of the download).

    -> go to Help Menu -> select "Restart with Add-ons Disabled"
    Firefox will close then it will open up with just basic Firefox. Now do this:
    -> go to Tools Menu -> Add-ons -> Extensions section -> REMOVE any Unwanted/Suspicious Extensions (add-ons) -> Restart Firefox
    -> Update ALL your Firefox Plug-ins https://www.mozilla.com/en-US/plugincheck/
    -> go to View Menu -> Zoom -> click "Reset"
    -> go to View Menu -> Page Style -> select "Basic Page Style"
    -> go Tools Menu -> Clear Recent History -> '''''Time range to clear: select EVERYTHING''''' -> click Details (small arrow) button -> place Checkmarks on ALL Options -> click "Clear Now"
    -> go to Tools Menu -> Options -> General -> '''''When Firefox starts : select "Show My Home Page"''''' -> Type the address of the website which you want to be your HomePage e.g. http:www.google.com -> click OK
    You can enable the Trustworthy Add-ons later. Check and tell if its working.

  • Problems viewing downloaded PDF files with Adobe Download Assist.

    How can I start using Adobe Down Load Assist, I need to view some forms in PDF format? & How can I change the way I view my forms?

    Are you trying to view PDF forms?  If so then you may want to use Adobe Reader.  You can find the download for Adobe Reader at http://get.adobe.com/reader/.

  • Download pdf-files from websites is disabled!???

    Some websites provide additional information by making the user download a PDF-file. For some reason (can't really say since when), whenever I need to download a PDF, either there's a blank location-address or Firefox just stops whenever the PDF-link has been found, without crashing though.
    I'd be happy to know how to resolve this problem.

    Far as I know the problem isn't on Apple's end. I'm download PDF files with no problem. But I've experienced what you are in the past and I think you were on the right track when you suspected Adobe. In the main Library folder (not the one in your personal directory) there's a folder called Internet Plugins. Look inside for a file called AdobePDFViewer and delete it. If I remember properly, you force Adobe Reader to install a new one by just starting Reader and it will ask if you want to install the PDF reader. Or, you might want to look at this plugin which, in my opinion, beats Adobe's plugin hands down.

  • Unable to download pdf files from websites

    For a month now I have been unable to dowload PDF Files from websites.
    I have un-installed Adobe Software & re-installed hoping this would solve
    this on going problem. But I am afraid it has not this problem seems to
    have accured since the latest up date of Sarfari I trust Apple are coming up with
    some sort of solution to this current problem ??

    Far as I know the problem isn't on Apple's end. I'm download PDF files with no problem. But I've experienced what you are in the past and I think you were on the right track when you suspected Adobe. In the main Library folder (not the one in your personal directory) there's a folder called Internet Plugins. Look inside for a file called AdobePDFViewer and delete it. If I remember properly, you force Adobe Reader to install a new one by just starting Reader and it will ask if you want to install the PDF reader. Or, you might want to look at this plugin which, in my opinion, beats Adobe's plugin hands down.

  • Is there any  function module to download pdf file

    is there any  function module to download pdf file
    WITH OUT GUI_DOWNLOAD FUNCTION MODULE.
    THANKS IN ADVANCED.

    Dear Laxman,
    Tryout the following Function Modules:
    (1)  CONVERT_ABAPSPOOLJOB_2_PDF --> convert abap spool output to PDF
    (2)  CONVERT_OTF --> Convert SAP documents (SAPScript) to other types.
    Example:
    CALL FUNCTION "CONVERT_OTF"
           EXPORTING    FORMAT                = "PDF"
           IMPORTING    BIN_FILESIZE          = FILE_LEN
           TABLES       OTF                   = OTFDATA
                        LINES                 = PDFDATA
           EXCEPTIONS   ERR_MAX_LINEWIDTH     = 1                  
                        ERR_FORMAT            = 2
                        ERR_CONV_NOT_POSSIBLE = 3
                        OTHERS                = 4.
    Regards,
    Abir
    Don't forget to award Points *

Maybe you are looking for

  • Macbook 5,2 to TV problems

    I have a Macbook 5,2 (model A1181) with Intel core 2 Duo, Nvidia Geforce 9400M. I purchased an Apple mini-DVI (D-Shape) to video adapter. I also purchased an S-Video cable to connect to my CRT TV. Although the mac flashes (blue ?) for a moment as if

  • Why GPS Takes long time to start in E90

    i do have an E90, everytime i start the MAP the GPS takes more than 10minute to start and sometimes it dosent start unles i plan a route so i start using external GPS is there any updates or a way to raise the performance of the GPS??

  • Some of my thumbnails have suddenly flipped 90 degrees counterclockwise. The underlying images are the still correct. Anyone seen this?

    I just upgraded to the latest version of Aperture and for some reason about a quarter of my thumbnails have suddenly shifted 90 degrees counterclockwise. I also deleted some of the image files that were in the trash.

  • Branch to different page based on the selection of select list

    Hi All, I have to create an application in which i have a form containing a field with static lov's ... now i want that whenever i click on a save button it branches to some page based on the selected value of lov of that field... like if i select ma

  • Pc's see eachother. Macbook doesn't

    I tried to search existing content and this is likely an easy one. the two pc's in my house see each other and can print on the same printer. My two macs don't see each other or the pc's. I've tried enabling sharing on all computers. Set up. Cat 5 fr