【grover】Rails로 PDF의 생성을 하는【2020년도판】
조금 살펴보면 wicked_pdf 나 prawn 등의 젬이 나옵니다.
이번에는 grover이라는 Puppeteer/Chromium을 사용하여 HTML에서 PDF와 이미지를 생성하는 젬의 존재를 알고 시도해 보았으므로 메모를 남깁니다.
절차
puppeteer 설치
npm install puppeteer
Gemfile에 작성
gem 'grover'
config/initializers/grover.rb에 다음을 추가합니다.
# frozen_string_literal: true
Grover.configure do |config|
config.options = {
format: 'A4',
margin: {
top: '5px',
bottom: '10cm'
},
viewport: {
width: 640,
height: 480
},
prefer_css_page_size: true,
emulate_media: 'screen',
cache: false,
timeout: 0, # Timeout in ms. A value of `0` means 'no timeout'
launch_args: ['--font-render-hinting=medium', '--lang=ja'], # 日本語表示のため --lang=ja を追加
wait_until: 'domcontentloaded'
}
end
controllers/api/sample_controller.rb 에 다음을 기술.
라우팅의 설명도 잊지 않고.
# frozen_string_literal: true
module Api
class SampleController < ApplicationController
include ActionController::MimeResponds # API モードで respond_to を使うために必要
def show
controller = ActionController::Base.new
html = controller.render_to_string(template: 'api/hoges/show', layout: 'pdf')
pdf = Grover.new(html).to_pdf
respond_to do |format|
format.html
format.pdf do
send_data(pdf, filename: 'your_filename.pdf', type: 'application/pdf')
end
end
end
pdf 생성을 위한 layout 파일 만들기
views/layouts/pdf.html.erb
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="content-type" content="text/html; charset=utf-8" />
<style>
</style>
</head>
<body>
<%= yield %>
</body>
</html>
api/sample/show.html.erb
<p>請求書</p>
<style>
p { font-size: 20px; }
</style>
결과
마지막으로
쉽게 pdf를 생성할 수 있었습니다.
처리 시간도 신경이 쓰이지 않는 레벨입니다.
Reference
이 문제에 관하여(【grover】Rails로 PDF의 생성을 하는【2020년도판】), 우리는 이곳에서 더 많은 자료를 발견하고 링크를 클릭하여 보았다
https://qiita.com/Hassan/items/70eb615b754b18709c9e
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
우수한 개발자 콘텐츠 발견에 전념
(Collection and Share based on the CC Protocol.)
npm install puppeteer
gem 'grover'
# frozen_string_literal: true
Grover.configure do |config|
config.options = {
format: 'A4',
margin: {
top: '5px',
bottom: '10cm'
},
viewport: {
width: 640,
height: 480
},
prefer_css_page_size: true,
emulate_media: 'screen',
cache: false,
timeout: 0, # Timeout in ms. A value of `0` means 'no timeout'
launch_args: ['--font-render-hinting=medium', '--lang=ja'], # 日本語表示のため --lang=ja を追加
wait_until: 'domcontentloaded'
}
end
# frozen_string_literal: true
module Api
class SampleController < ApplicationController
include ActionController::MimeResponds # API モードで respond_to を使うために必要
def show
controller = ActionController::Base.new
html = controller.render_to_string(template: 'api/hoges/show', layout: 'pdf')
pdf = Grover.new(html).to_pdf
respond_to do |format|
format.html
format.pdf do
send_data(pdf, filename: 'your_filename.pdf', type: 'application/pdf')
end
end
end
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="content-type" content="text/html; charset=utf-8" />
<style>
</style>
</head>
<body>
<%= yield %>
</body>
</html>
<p>請求書</p>
<style>
p { font-size: 20px; }
</style>
마지막으로
쉽게 pdf를 생성할 수 있었습니다.
처리 시간도 신경이 쓰이지 않는 레벨입니다.
Reference
이 문제에 관하여(【grover】Rails로 PDF의 생성을 하는【2020년도판】), 우리는 이곳에서 더 많은 자료를 발견하고 링크를 클릭하여 보았다
https://qiita.com/Hassan/items/70eb615b754b18709c9e
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
우수한 개발자 콘텐츠 발견에 전념
(Collection and Share based on the CC Protocol.)
Reference
이 문제에 관하여(【grover】Rails로 PDF의 생성을 하는【2020년도판】), 우리는 이곳에서 더 많은 자료를 발견하고 링크를 클릭하여 보았다 https://qiita.com/Hassan/items/70eb615b754b18709c9e텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
우수한 개발자 콘텐츠 발견에 전념 (Collection and Share based on the CC Protocol.)