Sample to create PDF from Excel with Ruby

background

Nowadays, it is common to create a program that extracts and organizes list-format information written in EXCEL and outputs it as PDF in form format. Before I forget, I want to describe the essence of the program.

Program overview

Read the list listed in EXCEL Create the following PDF files for the number of people in the list at once.

Click here for GitHub, a set of execution environment

image.png

Development environment

procedure

  1. EXCEL list
  2. Background image
  3. Japanese font
  4. Create a project
  5. Program code creation

1. EXCEL list

Create the following EXCEL file

--Filename and bus ./infiles/members.xlsx

--Sheet name 2021 message image.png

Column name Use
A Surname Output to card
B Name Output to card
C last name Used for file name
D first name Used for file name
E message Output to card

Sample name created with gem gimei

2. Background image

Prepare an image file with the following name ./infiles/background.png

image.pngサンプル(筆者撮影)

3. Japanese font

Download Japanese fonts for PDF output Put it in ./infiles/ipam.ttf

4. Project creation

Use bundler (fixed version, not required)

Gemfile


% bundle init

Add the following to the generated Gemfile

Gemfile


gem "roo", "~> 2.8.0"
gem "prawn", "~> 2.3.0"

Install below

% bundle install

Gemfile.lock is created

5. Program code creation

cows.rb


#Collect gems in Gemfile
require 'bundler'
Bundler.require

#OPEN EXCEL file
excel_path = './infiles/members.xlsx'
book = Roo::Spreadsheet.open(excel_path)

#Specify the sheet name(Not required)
sheet = book.sheet('2021 message')

#Confirmation of the last column and last row that exist
puts "Last column number: #{sheet.last_column}Last line number: #{sheet.last_row}"
# 'B3'Sheet to refer to a cell.cell(3, 2) or sheet.cell(3, 'B')

#Loop in the number of rows of the sheet. First line skipped as header
(2..sheet.last_row).each do |idx|

  #PDF initialization
  # A6(Postcard size)Sideways, no border
  pdf = Prawn::Document.new(
    page_size: "A6", page_layout: :landscape,
    top_margin: 0, bottom_margin: 0, left_margin: 0, right_margin: 0)

  #Loading Japanese fonts
  pdf.font "./infiles/ipam.ttf"

  #Background image Placed without borders
  pdf.image("./infiles/background.png ", at: [0,pdf.cursor])

  #Define drawing area
  pdf.bounding_box([10, pdf.cursor-10], width: 500) do
    # 'B3'Sheet to refer to a cell.cell(3, 2) or sheet.cell(3, 'B')

    #name
    pdf.font_size(20)
    sei = sheet.cell(idx, "A")
    mei = sheet.cell(idx, "B")
    pdf.text "#{sei} #{mei}Mr.", color: "ffffff"

    #Akeome
    pdf.move_down 15
    pdf.font_size(24)
    greet = "happy New Year"
    pdf.text greet, color: "ffffff"

    #message
    pdf.move_down 15
    pdf.font_size(14)
    message = sheet.cell(idx, "E")
    pdf.text message, color: "c9dfed"

    #check
    puts  " -- [#{idx}] #{sei} #{mei}Mr. msg: #{message}"
  end

  #Cow
  pdf.bounding_box([165, pdf.cursor-80], width: 500) do
    pdf.font_size(80)
    pdf.text "Year of ox",  color: "ff75b1"
  end

  #PDF save file name"outfiles/last_first.pdf"
  file_name = "./outfiles/#{sheet.cell(idx,"C")}_#{sheet.cell(idx,"D")}.pdf"
  pdf.render_file(file_name)

end

Folder structure

├── Gemfile
├── Gemfile.lock
├── cows.rb (Program code)
├── infiles
│   ├── background.png (Background image file)
│   ├── ipam.ttf (Japanese font)
│   └── members.xlsx (List file)
└── outfiles
    ├── hara_suzuka.pdf (Created PDF)
    ├── .....
    └── .....

When ruby ​​is executed below, pdf is created under ./outfies/

% ruby cows.rb

Reference site

https://github.com/roo-rb/roo https://github.com/prawnpdf/prawn https://prawnpdf.org/manual.pdf https://moji.or.jp/ipafont/ipaex00401/ https://github.com/willnet/gimei

Recommended Posts

Sample to create PDF from Excel with Ruby
From Java to Ruby !!
Sample to create GUI application with JavaFX + IntelliJ + Scene Builder
Create PDF with itext7-Free layout: Text-
Create PDF with itext7-Free layout: Table-
Create an excel file with poi
Introduction to Ruby (from other languages)
Create PDF with itext7 ~ Hello World ~
EXCEL file update sample with JAVA
Update MySQL from 5.7 to 8.0 with Docker
I tried to create an API to get data from a spreadsheet in Ruby (with service account)
Send an email from gmail with Ruby
Introduction to Ruby basic grammar with yakiniku
[Ruby] From the basics to the inject method
Create a MySQL environment with Docker from 0-> 1
Get started with "Introduction to Practical Rust Programming" (Day 4) Call Rust from Ruby
Sample to create custom tag for JSP
Procedure to create Ruby execution environment locally
I tried to build Ruby 3.0.0 from source
Sample to start Ubuntu with Deployment with client-go
From terminal to Ruby (standard input / output)
Convert JSON to TSV and TSV to JSON with Ruby
I tried to get the distance from the address string to the nearest station with ruby
How to create an Excel form using a template file with Spring MVC
Build a development environment to create Ruby on Jets + React apps with Docker
[Ruby] How to convert from lowercase to uppercase and from uppercase to lowercase
How to output Excel and PDF using Excella
[ruby] Create DHT11 / 22 class from scratch using datasheet
[Rails] rails new to create a database with PostgreSQL
Try to link Ruby and Java with Dapr
Setting to exit from Docker container with VScode
How to create multiple pull-down menus with ActiveHash
Text extraction in Java from PDF with pdfbox-2.0.8
Create jupyter notebook with Docker and run ruby
I want to play with Firestore from Rails
Try to get redmine API key with ruby
AtCoder ABC127 D hash to solve with Ruby 2.7.1
How to create member variables with JPA Model
[ruby] How to receive values from standard input?
Introduction to Ruby 2
JavaFX application development with IntelliJ IDEA and Gradle ~ From environment construction to sample code ~
Docker command to create Rails project with a single blow in environment without Ruby
Launch Docker from Java to convert Office documents to PDF
[Introduction] Try to create a Ruby on Rails application
3. Create a database to access from the web module
[Note] Apply password protection to Excel sheets with NPOI
How to make LINE messaging function made with Ruby
[Updated from time to time] Ruby on Rails Convenient methods
Converting TSV files to CSV files (with BOM) in Ruby
`Failed to create the host-only adapter` with docker-machine start`
How to output standard from an array with forEach
[Note] Create a java environment from scratch with docker
Ruby Regular Expression Extracts from a specific string to a string
[Ruby on Rails] From MySQL construction to database change
(Ruby on Rails6) How to create models and tables
Introduction to RSpec 4. Create test data with Factory Bot
I want to redirect sound from Ubuntu with xrdp
Ssh connect from container to git with VSCode Remote Container
How to get jdk etc from oracle with cli
A new employee tried to create an authentication / authorization function from scratch with Spring Security