Caracal::Document.save 'example.docx' do |docx|
# page 1
docx.h1 'Page 1 Header'
docx.hr
docx.p
docx.h2 'Section 1'
docx.p 'Lorem ipsum dolor....'
docx.p
docx.table @my_data, border_size: 4 do
cell_style rows[0], background: 'cccccc', bold: true
end
# page 2
docx.page
docx.h1 'Page 2 Header'
docx.hr
docx.p
docx.h2 'Section 2'
docx.p 'Lorem ipsum dolor....'
docx.ul do
li 'Item 1'
li 'Item 2'
end
docx.p
docx.img 'https://www.example.com/logo.png', width: 500, height: 300
end
需要注意的是
1、官方文档提供的docx.img
的图片可能会导致404 Not Found
错误
2、docx.table
的 @my_data
必须是一个二维数组,类似于[['Header 1','Header 2'],['Cell 1', 'Cell 2']]
添加以下代码到你的项目根目录下的Gemfile
中
gem 'caracal'
然后执行以下命令安装依赖
bundle install