This week's book giveaway is in the Programmer Certification forum.
We're giving away four copies of OCP Oracle Certified Professional Java SE 21 Developer Study Guide: Exam 1Z0-830 and have Jeanne Boyarsky & Scott Selikoff on-line!
See this thread for details.
  • 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
  • Tim Cooke
  • Liutauras Vilda
  • Jeanne Boyarsky
  • paul wheaton
Sheriffs:
  • Ron McLeod
  • Devaka Cooray
  • Henry Wong
Saloon Keepers:
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Tim Moores
  • Mikalai Zaikin
Bartenders:
  • Frits Walraven

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: 7645
178
  • 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: 28327
210
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.

 
Ew. You guys are ugly with a capital UG. Here, maybe this tiny ad can help:
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
reply
    Bookmark Topic Watch Topic
  • New Topic