Skip to content

Instantly share code, notes, and snippets.

@hidakatsuya
Last active January 17, 2017 07:58
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save hidakatsuya/6362423 to your computer and use it in GitHub Desktop.
Save hidakatsuya/6362423 to your computer and use it in GitHub Desktop.
Barby で作ったバーコードのサイズを調整する方法

Barby で作ったバーコードのサイズを調整するサンプルコード

動作確認環境

  • Mac OSX 10.8
  • Ruby 2.0.0-p195
  • ThinReports 0.7.6
  • barby 0.5.1
  • chunky_png 1.2.8

実行方法

  1. barcode.tlfadjust-barcode-size-by-barby-thinreports.rb を同じ場所にファイルとして保存
  2. adjust-barcode-size-by-barby-thinreports.rb を実行
    $ ruby adjust-barcode-size-by-barby-thinreports.rb
  3. barcode.pdf が作成される

参考

require 'thinreports'
require 'barby'
require 'barby/barcode/ean_13'
require 'barby/outputter/png_outputter'
barcode = Barby::EAN13.new('491234567890')
report = ThinReports::Report.new layout: 'barcode'
report.start_new_page do |page|
# Default
page.item(:barcode1).src(StringIO.new(barcode.to_png))
# width x2 (height is 100 by deafult)
page.item(:barcode2).src(StringIO.new(barcode.to_png(xdim: 2)))
# width x2 & height is 50
page.item(:barcode3).src(StringIO.new(barcode.to_png(xdim: 2, height: 50)))
end
report.generate_file 'barcode.pdf'
{"version":"0.7.6","finger-print":-1798017384,"config":{"title":"","option":{},"page":{"paper-type":"A4","orientation":"portrait","margin-top":"20","margin-bottom":"20","margin-left":"20","margin-right":"20"}},"svg":"<svg width=\"595.2\" height=\"841.8\" xmlns=\"http://www.w3.org/2000/svg\" xmlns:xlink=\"http://www.w3.org/1999/xlink\" preserveAspectRatio=\"none\" viewBox=\"0 0 595.2 841.8\"><g class=\"canvas\"><!--SHAPE{\"type\":\"s-iblock\",\"id\":\"barcode1\",\"display\":\"true\",\"desc\":null,\"box\":{\"x\":127,\"y\":20,\"width\":332.1,\"height\":210.1},\"position-x\":\"center\",\"position-y\":\"center\",\"svg\":{\"tag\":\"image\",\"attrs\":{\"x\":127,\"y\":20,\"width\":332.1,\"height\":210.1}}}SHAPE--><!--LAYOUT<g class=\"s-iblock\" x-display=\"true\" x-id=\"barcode1\" x-width=\"332.1\" x-height=\"210.1\" x-left=\"127\" x-top=\"20\" x-position-x=\"center\" x-position-y=\"center\"><rect class=\"s-iblock-box\" stroke=\"#7C4007\" fill=\"#f4e2c4\" stroke-width=\"0.28\" fill-opacity=\"0.8\" width=\"332.1\" height=\"210.1\" x=\"127\" y=\"20\"></rect><image width=\"32\" height=\"32\" class=\"s-iblock-mark\" opacity=\"0.5\" xlink:href=\"assets/icons/x-image-mark.png\" display=\"inline\" x=\"277\" y=\"109\"></image><text class=\"s-iblock-id\" font-size=\"11\" font-family=\"Helvetica\" font-weight=\"normal\" font-style=\"normal\" text-decoration=\"none\" text-anchor=\"start\" kerning=\"auto\" stroke=\"none\" fill=\"#7C4007\" fill-opacity=\"1\" x=\"129\" y=\"31\">barcode1</text></g>LAYOUT--><!--SHAPE{\"type\":\"s-iblock\",\"id\":\"barcode2\",\"display\":\"true\",\"desc\":null,\"box\":{\"x\":127,\"y\":245,\"width\":332.1,\"height\":210.1},\"position-x\":\"center\",\"position-y\":\"center\",\"svg\":{\"tag\":\"image\",\"attrs\":{\"x\":127,\"y\":245,\"width\":332.1,\"height\":210.1}}}SHAPE--><!--LAYOUT<g class=\"s-iblock\" x-display=\"true\" x-id=\"barcode2\" x-width=\"332.1\" x-height=\"210.1\" x-left=\"127\" x-top=\"245\" x-position-x=\"center\" x-position-y=\"center\"><rect class=\"s-iblock-box\" stroke=\"#7C4007\" fill=\"#f4e2c4\" stroke-width=\"0.28\" fill-opacity=\"0.8\" width=\"332.1\" height=\"210.1\" x=\"127\" y=\"245\"></rect><image width=\"32\" height=\"32\" class=\"s-iblock-mark\" opacity=\"0.5\" xlink:href=\"assets/icons/x-image-mark.png\" display=\"inline\" x=\"277\" y=\"334\"></image><text class=\"s-iblock-id\" font-size=\"11\" font-family=\"Helvetica\" font-weight=\"normal\" font-style=\"normal\" text-decoration=\"none\" text-anchor=\"start\" kerning=\"auto\" stroke=\"none\" fill=\"#7C4007\" fill-opacity=\"1\" x=\"129\" y=\"256\">barcode2</text></g>LAYOUT--><!--SHAPE{\"type\":\"s-iblock\",\"id\":\"barcode3\",\"display\":\"true\",\"desc\":null,\"box\":{\"x\":127,\"y\":470,\"width\":332.1,\"height\":210.1},\"position-x\":\"center\",\"position-y\":\"center\",\"svg\":{\"tag\":\"image\",\"attrs\":{\"x\":127,\"y\":470,\"width\":332.1,\"height\":210.1}}}SHAPE--><!--LAYOUT<g class=\"s-iblock\" x-display=\"true\" x-id=\"barcode3\" x-width=\"332.1\" x-height=\"210.1\" x-left=\"127\" x-top=\"470\" x-position-x=\"center\" x-position-y=\"center\"><rect class=\"s-iblock-box\" stroke=\"#7C4007\" fill=\"#f4e2c4\" stroke-width=\"0.28\" fill-opacity=\"0.8\" width=\"332.1\" height=\"210.1\" x=\"127\" y=\"470\"></rect><image width=\"32\" height=\"32\" class=\"s-iblock-mark\" opacity=\"0.5\" xlink:href=\"assets/icons/x-image-mark.png\" display=\"inline\" x=\"277\" y=\"559\"></image><text class=\"s-iblock-id\" font-size=\"11\" font-family=\"Helvetica\" font-weight=\"normal\" font-style=\"normal\" text-decoration=\"none\" text-anchor=\"start\" kerning=\"auto\" stroke=\"none\" fill=\"#7C4007\" fill-opacity=\"1\" x=\"129\" y=\"481\">barcode3</text></g>LAYOUT--></g></svg>","state":{"layout-guide":[]}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment