Win a copy of Micro Frontends in Action this week in the Server-Side JavaScript and NodeJS forum!
  • 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Paul Clapham
  • Bear Bibeault
  • Jeanne Boyarsky
Sheriffs:
  • Ron McLeod
  • Tim Cooke
  • Devaka Cooray
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Jj Roberts
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • salvin francis
  • Scott Selikoff
  • fred rosenberger

How can I write PDF from BLOB object?

 
Ranch Hand
Posts: 36
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I would like to write PDF file from SQL BLOB object. I created the code below based on:
https://coderanch.com/t/615356/databases/store-PDF-Word-BLOB-column
https://stackoverflow.com/questions/28220352/how-to-convert-a-blob-object-into-a-pdf-file-in-java



It creates a PDF but when I tried to open the PDF I got the message: format error: not a PDF or corrupted

Could someone let me know what I did wrong?

 
Saloon Keeper
Posts: 6600
161
Android Mac OS X Firefox Browser VI Editor Tomcat Server Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You store PDFs in a table called IMAGES in a column called Image? Highly confusing.

Why are you treating the data differently based on the filename for the target file? Presumably all data from the DB should be stored in the file, yes?

Have you tried something like this?
 
Beata Szabo-Takacs
Ranch Hand
Posts: 36
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Dear Tim,

The name of the BLOB column in SQL is "IMAGE" and the Table name is "IMAGES" because some .png images are stored as a binary large object.
I tried to create PDF in the same way as you suggested. I just commented out the "For PDF writing" part because the part of the code after "else" is the same as
you suggested. It creates PDF but when I open it I got the same format error.  
 
Tim Moores
Saloon Keeper
Posts: 6600
161
Android Mac OS X Firefox Browser VI Editor Tomcat Server Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Just to make sure: those blobs are in fact PDFs, not images? How are they being stored?
 
Beata Szabo-Takacs
Ranch Hand
Posts: 36
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Dear Tim,

I uploaded .png images to MySQL as longblob and I would like to download them as pdf. My aim is to convert the .png images to pdf.
 
Tim Moores
Saloon Keeper
Posts: 6600
161
Android Mac OS X Firefox Browser VI Editor Tomcat Server Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can't convert between file formats by changing the file extensions. You need to use a library that can create PDFs, like PDFBox. There is an example of how to create a PDF with an image on this site: https://coderanch.com/wiki/659953/PDFBox
 
Beata Szabo-Takacs
Ranch Hand
Posts: 36
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you very much Tim for your suggestion! I accepted. I could solve the issue with the following code:


 
I'd appreciate it if you pronounced my name correctly. Pinhead, with a silent "H". Petite ad:
Thread Boost feature
https://coderanch.com/t/674455/Thread-Boost-feature
reply
    Bookmark Topic Watch Topic
  • New Topic