Skip to content

Instantly share code, notes, and snippets.

@TheKidCoder
Last active August 29, 2015 14:04
Show Gist options
  • Save TheKidCoder/ddcc90ac9cd0f49bdd01 to your computer and use it in GitHub Desktop.
Save TheKidCoder/ddcc90ac9cd0f49bdd01 to your computer and use it in GitHub Desktop.
Using PostgresLO with Jruby & JDBC.
connection = ActiveRecord::Base.connection.raw_connection
manager = connection.connection.getLargeObjectAPI
oid = nil
#Write To LO
ActiveRecord::Base.transaction do
oid = manager.createLO
puts oid
path = java.nio.file.Paths.get("/Users/chris/Desktop/SIA - Arkansas/CORE - SIA - AR - Core Application.pdf")
array_buf = java.nio.file.Files.readAllBytes(path)
lo = manager.open(oid, Java::OrgPostgresqlLargeobject::LargeObjectManager::WRITE)
lo.truncate(0)
lo.write(array_buf)
size = lo.size
lo.close
puts size
end
#Read from LO
pdf_bytes = nil
ActiveRecord::Base.transaction do
lo = manager.open(oid.to_i)
pdf_bytes = lo.read(lo.size)
lo.close
end
reader = PDFS::IText::PDF::PdfReader.new(pdf_bytes)
puts reader.getNumberOfPages
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment