Last active
August 29, 2015 14:04
-
-
Save TheKidCoder/ddcc90ac9cd0f49bdd01 to your computer and use it in GitHub Desktop.
Using PostgresLO with Jruby & JDBC.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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