6
5

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

rails PDFファイルをマージするには Combine PDF gem

Posted at

gem combine_pdf

rails内でPDFファイルをマージ(コンバイン)したく探しました。
そしたら、ありました。

combine_pdf

こんな感じ

公式より抜粋
 pdf = CombinePDF.new
 pdf << CombinePDF.load("file1.pdf") # one way to combine, very fast.
 pdf << CombinePDF.load("file2.pdf")
 pdf.save "combined.pdf"

loadメソッドの引数はファイルパスのみ対応のようです。
pdf.saveで指定したファイルパスにマージ後のPDFファイルを生成します。

carrierwave+S3 railsでの使用例を書きに載せてみる。

hoge.rb(モデル)
class Hoge < ApplicationRecord
  mount_uploader :pdf, FileUploader
end 

pdf = CombinePDF.new

hoge = Hoge.find(1) # model Hoge
hogehoge = Hoge.find(2)

pdf_object1 = open(hoge.pdf_url)
pdf << CombinePDF.load(pdf_object1.path)

pdf_object2 = open(hogehoge.pdf_url)
pdf << CombinePDF.load(pdf_object2.path)
6
5
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
6
5

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?