3
2

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.

日本情報クリエイト EngineersAdvent Calendar 2016

Day 19

Heroku + PHP + wkhtmltopdf のサンプル

Last updated at Posted at 2016-12-18

まずは前回の記事を読んでね。

プログラミングをしている健全な男子たるもの、スクリーンショットに直接メモ書きしてローカル保存したくなるのは自然の法則と言えるでしょう。
でも都度々々アプリを起動するのは超面倒だし、ある程度の過去データーも編集して上書き保存したい。
ということで作ろう。

準備

前回の記事のコマンドを使用してherokuアプリをサックリ生成。

コマンド
$ curl -o- https://raw.githubusercontent.com/nanananamememe/open_heroku/master/make.sh | bash
出力結果
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
100   261  100   261    0     0   1003      0 --:--:-- --:--:-- --:--:--  1007
mkdir: my_site: File exists
Reinitialized existing Git repository in ~/my_site/.git/
Creating app... done, ⬢ hoge-piyo-99999
https://hoge-piyo-99999.herokuapp.com/ | https://git.heroku.com/hoge-piyo-99999.git
On branch master
nothing to commit, working directory clean
Counting objects: 4, done.
Delta compression using up to 4 threads.
Compressing objects: 100% (2/2), done.
Writing objects: 100% (4/4), 299 bytes | 0 bytes/s, done.
Total 4 (delta 0), reused 0 (delta 0)
remote: Compressing source files... done.
remote: Building source:
remote: 
remote: -----> PHP app detected
remote: -----> Bootstrapping...
remote: -----> Installing platform packages...
remote:        NOTICE: No runtime required in composer.lock; using PHP ^5.5.17
remote:        - apache (2.4.20)
remote:        - nginx (1.8.1)
remote:        - php (5.6.29)
remote: -----> Installing dependencies...
remote:        Composer version 1.2.4 2016-12-06 22:00:51
remote: -----> Preparing runtime environment...
remote:        NOTICE: No Procfile, using 'web: vendor/bin/heroku-php-apache2'.
remote: -----> Checking for additional extensions to install...
remote: -----> Discovering process types
remote:        Procfile declares types -> web
remote: 
remote: -----> Compressing...
remote:        Done: 13.5M
remote: -----> Launching...
remote:        Released v3
remote:        https://hoge-piyo-99999.herokuapp.com/ deployed to Heroku
remote: 
remote: Verifying deploy.... done.
To https://git.heroku.com/hoge-piyo-99999.git
 * [new branch]      master -> master

ディレクトリ名を変更、名前はスクリーンショットにメッセージ書くのでss_messengerに。cdで移動。

コマンド
$ mv my_site ss_messenger
$ cd ss_messenger

MySQL(ClearDB)アドオン追加。下記コマンドを実行。

コマンド
$ heroku addons:create cleardb:ignite
出力結果
Creating cleardb:ignite on ⬢ hoge-piyo-99999... free
Created cleardb-aerodynamic-24359 as CLEARDB_DATABASE_URL
Use heroku addons:docs cleardb to view documentation

DBちゃんと作れたか、確認。

コマンドを叩く。

コマンド
$ heroku addons:info cleardb
出力結果
=== cleardb-hogehoge-99999
Attachments:  hoge-piyo-99999::CLEARDB_DATABASE
Installed at: Tue Dec 13 2016 22:05:25 GMT+0900 (JST)
Owning app:   hoge-piyo-99999
Plan:         cleardb:ignite
Price:        free
State:        created

作成途中だと上記のStateの値がcreatingになる。

PHPからDB接続ができるか確認。

公式ドキュメントにphpのサンプルがあるのでそれを参考に。
https://devcenter.heroku.com/articles/cleardb#using-cleardb-with-php

コマンド
$ rm index.html;
$ echo '<?php' > index.php;
$ echo 'extract(parse_url(getenv("CLEARDB_DATABASE_URL")));' >> index.php;
$ echo 'var_dump((new mysqli($host,$user,$pass,substr($path,1))->client_version));' >> index.php;
$ git add .;
$ git commit -a -m 'db test';
$ git push heroku master;
$ heroku open;
出力結果
int(50012)

ビルドパッケージにwkhtmltopdfを追加。

複数ビルドパッケージを利用できるよう heroku/heroku-buildpack-multi を使用。

コマンド
$ heroku buildpacks:set 'https://github.com/heroku/heroku-buildpack-multi.git';
$ echo 'https://github.com/heroku/heroku-buildpack-php.git' >> .buildpacks;
$ echo 'https://github.com/rafaelp/heroku-buildpack-wkhtmltopdf.git' >> .buildpacks;
$ git add .;
$ git commit -m 'Add multi-buildpack';
$ git push heroku master;
一部出力結果
remote: Compressing source files... done.
remote: Building source:
remote: 
remote: -----> Multipack app detected
remote: =====> Downloading Buildpack: https://github.com/heroku/heroku-buildpack-php.git
remote: =====> Detected Framework: PHP
remote: -----> Bootstrapping...
remote: -----> Installing platform packages...
remote:        NOTICE: No runtime required in composer.json; requirements
remote:        from dependencies in composer.lock will be used for selection
remote:        - php (7.1.0)
remote:        - apache (2.4.20)
remote:        - nginx (1.8.1)
remote: -----> Installing dependencies...
remote:        Composer version 1.2.4 2016-12-06 22:00:51
remote:        Loading composer repositories with package information
remote:        Installing dependencies from lock file
remote:          - Installing symfony/process (v3.2.1)
remote:            Loading from cache
remote:        
remote:          - Installing knplabs/knp-snappy (0.4.3)
remote:            Loading from cache
remote:        
remote:        Generating optimized autoload files
remote: -----> Preparing runtime environment...
remote:        NOTICE: No Procfile, using 'web: vendor/bin/heroku-php-apache2'.
remote: -----> Checking for additional extensions to install...
remote: =====> Downloading Buildpack: https://github.com/rafaelp/heroku-buildpack-wkhtmltopdf.git
remote: =====> Detected Framework: wkhtmltopdf
remote: -----> Installing wkhtmltopdf 0.12.2.1
remote:        Writing profile script
remote: Using release configuration from last framework (wkhtmltopdf).
remote: -----> Discovering process types
remote:        Procfile declares types -> web
remote: 
remote: -----> Compressing...
remote:        Done: 56.8M
remote: -----> Launching...
remote:        Released v11
remote:        https://hoge-piyo-99999.herokuapp.com/ deployed to Heroku
remote: 
remote: Verifying deploy.... done.

インストールできたか確認。

コマンド
$ heroku run wkhtmltoimage -V
出力結果
Running wkhtmltoimage -V on ⬢ hoge-piyo-99999... up, run.2087 (Free)
wkhtmltoimage 0.12.2.1 (with patched qt)

wkhtmltopdfの操作用にcomposerにsnappy追加。

vendor の中身を push しないよう .gitignore に vendor を追加。
ついでに動作の確認も

コマンド
$ echo 'vendor' > ./.gitignore;
$ composer require knplabs/knp-snappy;
$ echo '<?php' > index.php;
$ echo 'require "vendor/autoload.php";' >> index.php;
$ echo 'use Knp\Snappy\Image;' >> index.php;
$ echo '$snappy = new Image("wkhtmltoimage");' >> index.php;
$ echo 'header("Content-Type: image/jpeg");' >> index.php;
$ echo 'echo $snappy->getOutput("http://www.github.com");' >> index.php;
$ git add .;
$ git commit -a -m wkhtmltoimage;
$ git push heroku master;
$ heroku open;

ブラウザに画像に変換されたgithubのサイトが表示されれば成功。

とりあえず今回はここまで、続きは次回に。

3
2
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
3
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?