Ruby:通过REST API下载PDF发票(e-conomic)

时间:2018-01-28 13:17:20

标签: ruby rest pdf download

我试图通过我的会计门户网站上的Ruby脚本下载PDF发票,e-conomic。我使用REST API,并且能够找到发票PDF的URL。

但是,我不确定如何下载它。如果我直接引用" GET"使用PDF URL的语句,我能够打印大量非感知数据(可能是Ruby输出中表示的PDF本身)。

我知道如何将PDF文件下载到PC上的本地文件夹中吗?

谢谢,

马丁

RUBY CODE:使用下面的内容打印非感知输出:

hHeader = {" X-AppSecretToken" => ' [TOKEN]'," X-AgreementGrantToken" => ' [TOKEN]'," Content-Type" => '应用/ JSON'} put hTest = RestClient.get(" https://restapi.e-conomic.com/invoices/booked/10000/pdf",hHeader)

2 个答案:

答案 0 :(得分:2)

哎呀,我回答错了问题。您可以下载pdf文件并将其写入本地文件,如下所示:

File.open('some_name.pdf', 'wb') do |local_f|
  open('http://unec.edu.az/application/uploads/2014/12/pdf-sample.pdf', 'rb') do |pdf|
    local_f << pdf.read
  end
end

======

  

我能够打印很多非感知数据(可能是PDF本身   在Ruby输出中表示。)

是的,你不能像纯文本文件一样阅读pdf文件。 pdf文件是特殊格式的文件。您可以使用pdf-reader gem解码pdf文件:

require 'pdf-reader'
require 'open-uri'
require 'pp'

open('http://unec.edu.az/application/uploads/2014/12/pdf-sample.pdf') do |f|
  p f.read(100)  #Gibberish

  reader = PDF::Reader.new(f)
  pp reader.info  #A hash

  reader.pages.each do |page|
    puts page.text
  end

end

--output:--
"%PDF-1.3\r%\xE2\xE3\xCF\xD3\r\n7 0 obj\r<</Linearized 1/L 7945/O 9/E 3524/N 1/T 7656/H [ 451 137]>>\rendobj\r         "

{:Author=>"cdaily",
 :CreationDate=>"D:20000629102108+11'00'",
 :Creator=>"Microsoft Word 8.0",
 :ModDate=>"D:20131028152413-04'00'",
 :Producer=>"Acrobat Distiller 4.0 for Windows",
 :Title=>"This is a test PDF file"}


                         Adobe Acrobat PDF Files

Adobe® Portable Document Format (PDF) is a universal file format that preserves all
of the fonts, formatting, colours and graphics of any source document, regardless of
the application and platform used to create it.


Adobe PDF is an ideal format for electronic document distribution as it overcomes the
problems commonly encountered with electronic file sharing.

•   Anyone, anywhere can open a PDF file. All you need is the free Adobe Acrobat
    Reader. Recipients of other file formats sometimes can't open files because they
    don't have the applications used to create the documents.


•   PDF files always print correctly on any printing device.

•   PDF files always display exactly as created, regardless of fonts, software, and
    operating systems. Fonts, and graphics are not lost due to platform, software, and
    version incompatibilities.


•   The free Acrobat Reader is easy to download and can be freely distributed by
    anyone.

•   Compact PDF files are smaller than their source files and download a
    page at a time for fast display on the Web.

答案 1 :(得分:1)

谢谢!

我最终使用以下代码直接保存文件:

打开(&#39; C:\ Ruby24-x64 \ myfile.pdf&#39;,&#39; wb&#39;){| f | f.puts(pdfData)}