二进制列条目在 RAILS 中转换为 .pdf 文件
Binary column entry converted to .pdf file in RAILS
如何从 rails 数据库条目中检索我的 PDF 二进制数据并将其输出为 PDF 格式?这是我目前得到的,但这只是显示和空的 PDF 文件:
def download
@submissions = Submission.all
# Create .pdf from binaries
@submissions.each do |sub|
@filePath = Rails.root.join('tmp/system/', sub.id.to_s, sub.file_file_name)
logger.error('Filepath here: ' + @filePath.to_s)
@content = Attachment.find_by! submission_id: 174
File.open(@filePath.to_s, 'w:binary') do |out|
out.write(@content.file_contents)
end
end
为什么这没有在 PDF 文档中写入任何内容?
一般写出来即可:
File.open('/tmp/whatever.pdf', 'w:binary') do |out|
out.write(model.file_content)
end
请记住,如果您需要真正的临时文件,请使用 Tempfile:
file = Tempfile.new
file.write(model.file_content)
file.close
要使用此文件:
do_something_with_pdf(file.path)
完成后,将其删除:
file.unlink
在这里回答:
# Download zip file of all submission
def download
@submissions = Submission.all
# Create .pdf from binaries
@submissions.each do |sub|
@filePath = Rails.root.join('tmp/system/', sub.id.to_s)
Dir.mkdir(@filePath.to_s)
@content = Attachment.find_by! submission_id: sub.id
File.open((@filePath.to_s + '/' + sub.file_file_name.to_s), 'w+b') do |out|
out.write(@content.file_contents)
end
end
end
如何从 rails 数据库条目中检索我的 PDF 二进制数据并将其输出为 PDF 格式?这是我目前得到的,但这只是显示和空的 PDF 文件:
def download
@submissions = Submission.all
# Create .pdf from binaries
@submissions.each do |sub|
@filePath = Rails.root.join('tmp/system/', sub.id.to_s, sub.file_file_name)
logger.error('Filepath here: ' + @filePath.to_s)
@content = Attachment.find_by! submission_id: 174
File.open(@filePath.to_s, 'w:binary') do |out|
out.write(@content.file_contents)
end
end
为什么这没有在 PDF 文档中写入任何内容?
一般写出来即可:
File.open('/tmp/whatever.pdf', 'w:binary') do |out|
out.write(model.file_content)
end
请记住,如果您需要真正的临时文件,请使用 Tempfile:
file = Tempfile.new
file.write(model.file_content)
file.close
要使用此文件:
do_something_with_pdf(file.path)
完成后,将其删除:
file.unlink
在这里回答:
# Download zip file of all submission
def download
@submissions = Submission.all
# Create .pdf from binaries
@submissions.each do |sub|
@filePath = Rails.root.join('tmp/system/', sub.id.to_s)
Dir.mkdir(@filePath.to_s)
@content = Attachment.find_by! submission_id: sub.id
File.open((@filePath.to_s + '/' + sub.file_file_name.to_s), 'w+b') do |out|
out.write(@content.file_contents)
end
end
end