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

llycmphe  于 2023-08-04  发布在  Ruby
关注(0)|答案(2)|浏览(103)

我试图通过Ruby脚本从我的会计门户网站e-ecomic下载PDF发票。我使用REST API,并且能够找到发票PDF的URL。
但是我不知道如何下载它。如果我直接用PDF URL引用一个“GET”语句,我就能够打印很多无意义的数据(可能是Ruby输出中表示的PDF本身)。
如何将PDF文件下载到PC上的本地文件夹?
谢谢你,
马田
RUBY CODE:使用下面的打印无意义输出,如前所述:

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

字符串

5t7ly7z5

5t7ly7z51#

哎呀,我回答错问题了。你可以下载一个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

字符串

我能够打印大量无意义的数据(可能是Ruby输出中表示的PDF本身)。
是的,你不能像阅读纯文本文件那样阅读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.

zynd9foi

zynd9foi2#

谢谢你,谢谢
我最终使用以下代码直接保存文件:
public('C:\Ruby24-x64\myfile.pdf',' wb'){|关闭|f.puts(pdfData)}

相关问题