我有一个场景,我需要根据对象数组生成多页pdf。
以下是我的模特:
# Member has_many certificates
class Member < ActiveRecord::Base
has_many :certificates, dependent: :destroy
before_save :set_expiry
def self.delete_expired
where(expire_at: Date.today).destroy_all
end
private
def set_expiry
self.expire_at = 15.days.from_now
end
end
# certificate belongs_to member
class Certificate < ActiveRecord::Base
belongs_to :member
belongs_to :certificate_type
belongs_to :company
def company_name
company.name
end
def page_orientation
company_name == 'CompanyA' ? 'Landscape' : 'Portrait'
end
def page_height
company_name == 'CompanyA' ? '10.5in' : '11in'
end
def page_width
company_name == 'CompanyA' ? '7in' : '8.5in'
end
end
# each type can have many certificates
class CertificateType < ActiveRecord::Base
has_many :certificates
end
用户流程如下:
我有一个自定义表单,我在其中创建证书对象(复选框选择),然后提交表单。控制器接收证书对象的散列。我必须逐个遍历所有对象,为它的相关成员创建证书。问题是每个证书都有自己的模板和一些自定义页面布局设置,如方向,宽度和高度。我想生成一个包含多个页面的单个pdf文件,其中每个页面都是一个证书。我被困在哪里以及如何通过每个证书的页面布局设置。我在我的控制器中尝试了render_to_string并尝试在那里设置页面布局选项,但它似乎不起作用。我需要一些建议或指示。
答案 0 :(得分:4)
建立@fdisk的答案,但使用Ruby原生combine_pdf
gem ......你可以尝试:
require 'combine_pdf'
merged_certificates = CombinePDF.new
@member.certificates.each do |certificate|
pdf_data = WickedPdf.new.pdf_from_string(render_to_string(something, layout: some_layout))
merged_certificates << CombinePDF.parse(pdf_data)
end
send_data merged_certificates.to_pdf, filename: 'mypdf.pdf' #...
答案 1 :(得分:0)
由于这是一个选项,我建议您通过发行版的软件包管理器(yum
,apt-get
,dnf
等)安装Tempfile
。之后,将每个证书保存为require 'shellwords'
certificates = @member.certificates.map do |certificate|
Tempfile.open(["cert", ".pdf"]) { |file| file << WickedPdf.new.pdf_from_string(render_to_string(something, layout: some_layout)) }
end
paths = certificates.map { |cert| cert.path.shellescape }
@final_pdf = Tempfile.new(["cert", ".pdf"])
@final_pdf.close
%x[pdfunite #{paths} #{@final_pdf.path.shellescape}]
:
@final_pdf
该 var myArray = ["cat","dog","bird"];
var myString = "There is a cat looking the bird and a dog looking the cat";
var myString2 = "There is a cat and a dog looking one each other";
var myString3 = "There is a classcat and a iddog looking at bird";
var myString4 = "There is a catclass and a dog looking at bird";
document.write(myArray.every(function(word) {
return (new RegExp('\\w*' + word + '\\b').test(myString));
}) + '<br>');
document.write(myArray.every(function(word) {
return (new RegExp('\\w*' + word + '\\b').test(myString2));
}) + '<br>');
document.write(myArray.every(function(word) {
return (new RegExp('\\w*' + word + '\\b').test(myString3));
}) + '<br>');
document.write(myArray.every(function(word) {
return (new RegExp('\\w*' + word + '\\b').test(myString4));
}));
对象应包含合并的单个证书。我还做了一个简单的gem来处理这些情况,但它可能会有一些你不想要的额外开销(它的目的是创建带有嵌套书签的组合PDF文件)。