Upgrade to Pro
— share decks privately, control downloads, hide ads and more …
Speaker Deck
Features
Speaker Deck
PRO
Sign in
Sign up for free
Search
Search
[Roppongi.rb#20] groverのコードをなんとなく読んでみた
Search
ryichk
July 11, 2024
Programming
1
130
[Roppongi.rb#20] groverのコードをなんとなく読んでみた
Roppongi.rb#20
でLTしたスライドです!
ryichk
July 11, 2024
Tweet
Share
More Decks by ryichk
See All by ryichk
OAuth2.0、JWT 入門 / Introduction to OAuth2.0 and JWT
ryichk
0
39
「入門 監視」を読んでみた / I read "Practical Monitoring"
ryichk
0
55
Lambdalithという選択肢を検討中 / Considering the option of Lambdalith
ryichk
1
460
Other Decks in Programming
See All in Programming
Development of an App for Intuitive AI Learning - Blockly Summit 2025
teba_eleven
0
120
Practical Tips and Tricks for Working with Compose Multiplatform Previews (mDevCamp 2025)
stewemetal
0
130
GoのGenericsによるslice操作との付き合い方
syumai
2
660
業務自動化をJavaとSeleniumとAWS Lambdaで実現した方法
greenflagproject
1
120
コードの90%をAIが書く世界で何が待っているのか / What awaits us in a world where 90% of the code is written by AI
rkaga
41
26k
Elixir で IoT 開発、 Nerves なら簡単にできる!?
pojiro
1
150
Gleamという選択肢
comamoca
6
740
ドメインモデリングにおける抽象の役割、tagless-finalによるDSL構築、そして型安全な最適化
knih
11
1.9k
FormFlow - Build Stunning Multistep Forms
yceruto
1
180
統一感のある Go コードを生成 AI の力で手にいれる
otakakot
0
3k
Spring gRPC で始める gRPC 入門 / Introduction to gRPC with Spring gRPC
mackey0225
2
510
Claude Codeの使い方
ttnyt8701
1
120
Featured
See All Featured
[RailsConf 2023 Opening Keynote] The Magic of Rails
eileencodes
29
9.5k
Easily Structure & Communicate Ideas using Wireframe
afnizarnur
194
16k
Raft: Consensus for Rubyists
vanstee
140
7k
Why You Should Never Use an ORM
jnunemaker
PRO
56
9.4k
Balancing Empowerment & Direction
lara
1
330
Making Projects Easy
brettharned
116
6.2k
Git: the NoSQL Database
bkeepers
PRO
430
65k
YesSQL, Process and Tooling at Scale
rocio
172
14k
Improving Core Web Vitals using Speculation Rules API
sergeychernyshev
16
940
The Success of Rails: Ensuring Growth for the Next 100 Years
eileencodes
45
7.4k
How to Think Like a Performance Engineer
csswizardry
24
1.7k
The Invisible Side of Design
smashingmag
299
51k
Transcript
groverのコードを なんとなく読んでみた Ryo Ichiki
自己紹介 3社目:HRテック系ベンチャー(現在) Ruby, TypeScript 2社目:不動産テック系ベンチャー Ruby, PHP, JavaScript 1社目:小さい受託開発会社 C#
大学:理学部(情報系ではない) C, R, Ruby GitHub:ryichk X:@ryichk_dev
groverとは? HTMLをPDFやPNG画像、JPEG画像に変換できるgemです。 内部でPuppeteerとChromiumを使って変換しています。 PuppeteerはChromeを操作するNode.jsのライブラリです。 なのでgroverを使うためにはNode.jsの環境も必要です。 https://github.com/Studiosity/grover
groverを知ったきっかけ HTMLをPDFに変換するメジャーなgemとしてwicked_pdfがあります。 このwicked_pdfを別のgemにリプレイスする必要がありました。 wicked_pdfは内部でwkhtmltopdfというライブラリ(gemではない)を使っています。 wkhtmltopdfは2023/1/2にアーカイブされておりメンテが終了している状態です。 EC2のOSをAmazon Linux 2023へアップグレードした際に動かなくなるリスクがあり、仮 に動いたとしても...
groverの他にも候補がありました ・ferrum ・prawn
Groverの使い方 https://github.com/Studiosity/grover?tab=readme-ov-file#usage
Railsで使う場合 https://github.com/Studiosity/grover?tab=readme-ov-file#from-a-view-template
Grover#to_pdf を見ていく
Grover#to_pdf 〜
Grover::Processor#convert
Grover::Processor#convert
Grover::Processor#spawn_process
Open3.#popen3について popen3(*cmd) -> [IO, IO, IO, Thread] 外部プログラムcmdを実行し、そのプロセスの標準入力、標準出力、標準エラー出力に 接続されたパイプと実行したプロセスを待つためのスレッドを4要素の配列で返す。 https://docs.ruby-lang.org/ja/latest/method/Open3/m/popen3.html
Open3.#popen3 を使って別プロセスを立ち上げ、 Node.js 経由で processor.cjs を実行している
Grover::Processor#convert
Grover::Processor#ensure_packages_are_initiated
Grover::Processor#ensure_packages_are_initiated 要はNode.jsのプロセスが問題なく起 動しているか確認している
Grover::Processor#convert
Grover::Processor#call_js_method
Grover::Processor#call_js_method
Grover::Processor#call_js_method つまり、 processor.cjsに method, url_or_html, options を渡している method = :pdf
processor.cjs(超ざっくり) 1. puppeteerでChromeブラウザを起動し、接続する 2. 引数で渡されたURL or HTML文字列をブラウザで読み込む 3. puppeteerのPage.pdf()メソッドを実行 a.
読み込んだページの PDFを生成する 4. 生成されたPDFを標準出力に書き出す 5. Chromeブラウザの接続を閉じて終了
まとめ・感想 ・groverはpuppeteerというNode.jsのライブラリを Ruby経由で無理やり操作するためのgemという感じ ・やっていることはシンプル。 ・だけど細かく見ていくと複雑 ・Ruby経由でNode.jsのライブラリを実行できるなら他にも色々できそう (Node.jsに限らず)
ご清聴ありがとうございました