Arquivo da tag: PDF

PDFBox – How to read PDF file in Java

pom.xml
[code type=xml]

org.apache.pdfbox
pdfbox
2.0.6

Class
[code type=java]
import org.apache.pdfbox.pdmodel.PDDocument;
import org.apache.pdfbox.text.PDFTextStripper;
import org.apache.pdfbox.text.PDFTextStripperByArea;

import java.io.File;
import java.io.IOException;

public class ReadPdf {

public static void main(String[] args) throws IOException {

try (PDDocument document = PDDocument.load(new File(“/path-to/abc.pdf”))) {

document.getClass();

if (!document.isEncrypted()) {

PDFTextStripperByArea stripper = new PDFTextStripperByArea();
stripper.setSortByPosition(true);

PDFTextStripper tStripper = new PDFTextStripper();

String pdfFileInText = tStripper.getText(document);
//System.out.println(“Text:” + st);

// split by whitespace
String lines[] = pdfFileInText.split(“\\r?\\n”);
for (String line : lines) {
System.out.println(line);
}

}

}

}
}

PDFBox – How to read PDF file in Java

Rails – How to convert PDF to Excel or CSV

In Rails 4:

require 'httmultiparty'
class PageTextReceiver
  include HTTMultiParty
  base_uri 'http://localhost:3000'
 
  def run
    response = PageTextReceiver.post('https://pdftables.com/api?key=myapikey', :query => { f: File.new("/path/to/pdf/uploaded_pdf.pdf", "r") })
 
    File.open('/path/to/save/as/html/response.html', 'w') do |f|
      f.puts response
    end
  end
 
  def convert
    f = File.open("/path/to/saved/html/response.html")
    doc = Nokogiri::HTML(f)
    csv = CSV.open("path/to/csv/t.csv", 'w',{:col_sep => ",", :quote_char => '\'', :force_quotes => true})
    doc.xpath('//table/tr').each do |row|
      tarray = []
      row.xpath('td').each do |cell|
        tarray << cell.text
      end
      csv << tarray
    end
    csv.close
  end
end

http://stackoverflow.com/questions/30183738/how-to-convert-pdf-to-excel-or-csv-in-rails-4