• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • paul wheaton
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Tim Cooke
  • Liutauras Vilda
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Piet Souris
Bartenders:
  • salvin francis
  • Mikalai Zaikin
  • Himai Minh

Generate Zip File - download PDF from Url and Generate Zip to Download in Browser - rest service

 
Ranch Hand
Posts: 44
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello All,
 I have create Rest Service and I am trying to Generate Zip file. This Zip file created from muliple PDF files which are downloaded using method
 InputStream inpuStream =  new URL(url).openStream(). I am able to Generate Zip file Which included PDF files but PDF files are broken.
 Even If i try to Generate it from String its coming as broken PDF and i am getting Error message "Not a supported File Type or file is broken or damaged"
 
 I have given my code below for your reference. Could you please Guide me where I am making the mistake. Do I need to return ResponsEntity or void controller is fine.

Rest controller:


2)Service method:

 
 
Saloon Keeper
Posts: 6919
163
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Even If i try to Generate it from String its coming as broken PDF


PDFs are binary files, if you ever treat them as character data, they'll be broken.

new ByteArrayInputStream( "this is test to generarte pdf test file this is test tdfsfs this is test to generarte pdf test file this is test tdfsfs"


That's not a PDF, that's character data. It doesn't become a PDF by storing it in a file and giving it a .pdf file extension. You'll need to create a proper PDF somehow, and then treat it as binary data.
 
Saloon Keeper
Posts: 23515
161
Android Eclipse IDE Tomcat Server Redhat Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The proper MIME type (ContentType) for ZIP files is application/zip. The client will probably not handle a ZIP file properly otherwise. Note also that MIME types are conventionally given in lower-case, not upper-case.

While it's not immediately obvious to me, others here are of the impression that you're attempting also to create PDF files on the fly as though PDF was straight text. PDF is a rigorously-defined file format containing encapsulated PostScript commands and metadata and unless you're a REAL glutton for punishment, you need a good PDF-creation library if you want to build PDFs yourself.

If you're simply trying to package a set of uploaded PDF files into a ZIP, then you don't need a PDF library, but you do need to make sure that if there's an option for the file in the ZIP utilities that you select "binary" and not "text". PDF in its original form was indeed text. but it's not intended to be treated as text, and in particular it isn't likely to take kindly to how some utilities translate end-of-line characters when they process "text" files.

 
Onion rings are vegetable donuts. Taste this tiny ad:
ScroogeXHTML - a fast and small RTF to HTML5 and XHTML converter library
https://coderanch.com/t/742149/ScroogeXHTML-RTF-HTML-XHTML-converter
reply
    Bookmark Topic Watch Topic
  • New Topic