生成二維碼PDF合併PDF壓縮

model


class Model
	def created_pdf
	    tmp_dir = 'tmp_file/'+Time.now.strftime('%Y%m%d')
	    file_dir = Rails.root.join('public', tmp_dir)
	    FileUtils.mkdir_p file_dir
	    file_name = Time.now.strftime('%H%M%S')+(0...8).map { (65 + rand(26)).chr }.join
	    file_path = file_dir.join(file_name + '.pdf')

	    qrcode_file_path = file_dir.join(file_name + 'qrcode.png')

	    qr = RQRCode::QRCode.new('test-msg', :size => 2, :level => :m)
	    png = qr.to_img
	    png.resize(90, 90).save(qrcode_file_path)

	    pdf = WickedPdf.new.pdf_from_string(erb_html(qrcode_file_path), {
	        page_width: 100,
	        page_height: 150,
	        margin: {top: 5, bottom: 0, left: 5, right: 5}
	    })

	    File.open(file_path, 'wb'){|file| file << pdf }

	    File.delete file_path if file_path.present?
	    File.delete qrcode_file_path if qrcode_file_path.present?

	    return pdf
	end

	def erb_html qrcode_file_path
	    erb_template_path = Rails.root.join('app', 'views', 'models', 'template.html.erb')
	    image_source_path = Rails.root.join('app', 'views', 'models', 'img')
	    erb_template = File.read(erb_template_path)
	    variable = { qrcode_file_path: qrcode_file_path, xxxxxxxx }
	    html = ERB.new(erb_template).result(OpenStruct.new(variable).instance_eval{binding})
	    return html
	end

	def zip_data
		file_dir = Rails.root.join('public', 'upload_tmp_file', Time.now.strftime('%Y%m%d'))
		FileUtils.mkdir_p file_dir

		data = Zip::OutputStream.write_buffer do |out|
			extract_pdf file_dir, out, self
		end
	end

	def extract_pdf file_dir, out, model
		_pdf = CombinePDF.new
		_pdf << CombinePDF.parse(model.created_pdf)
		_pdf << CombinePDF.parse(Net::HTTP.get_response(URI.parse(model.url_pdf)).body)
		
		path = file_dir.join("pdf_#{Time.now.to_i}.pdf")
		_pdf.save path

		write_zip out, path, "pdf-#{Time.now.strftime('%Y%m%d%H%M%S')}.pdf"
	end

	def write_zip out, path, name
		pdf = open(path).read
		out.put_next_entry(name)
		out.write pdf
	end
end

controller

data = Model.new.zip_data

send_data data.string, type: 'application/zip', filename: "Posti_Shippment_Labels_#{Time.now.strftime("%d-%m-%Y")}.zip"

template.html.erb

<!DOCTYPE html>
<html>
  <head>
    <meta charset="utf-8">
    <title>發貨明細單</title>
    <meta content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=0" name="viewport">
    <meta name="apple-mobile-web-app-capable" content="no" />
    <meta name="format-detection" content="telephone=no" />
    <meta name="format-detection" content="email=no" />
    <meta name="apple-mobile-web-app-status-bar-style" content="default">
    <style>
      *{
        margin: 0;
        padding: 0;
      }
      body {
        line-height:1;
      }
      .index{
        width: 255.32pt;
        height: 395pt;
        background-color: white;
        margin:0 auto;
      }
    </style>
  </head>
  <body style="font-family: '宋體';">
    <div class="index">
      <div class="top" style="width: 100%;">
        <div style="width: 100%; height: 16pt; position: relative">
          <div style="text-align: center;">
            <p style="font-size: 14pt">發貨明細單</p>
          </div>
        </div>
        <div >
          <div style="height: 120pt; line-height: 16pt; margin-top: 10pt;">
            <div>
              <p style="font-family: '宋體';font-size: 12pt;font-weight: bold">訂單信息</p>
            </div>
            <div style=" position: relative; margin-top: 10pt;">
              <div style="width: 55%;position: absolute;">
                <p style="font-family: '宋體';font-size: 8pt;line-height: 16pt;">訂單號: <%= order.code %></p>
                <p style="font-family: '宋體';font-size: 8pt;line-height: 16pt;">姓名: <%= order.receiver_name.split('').map.each_with_index{|x, index| index.zero? ? x : '*'}.join %></p>
                <p style="font-family: '宋體';font-size: 8pt;line-height: 16pt;">手機號碼: <%= order.phone.reverse.split('').map.each_with_index{|x, index| (index >= 4 && index <= 7) ? '*' : x}.join.reverse %></p>
                <p style="font-family: '宋體';font-size: 8pt;line-height: 16pt;">地址: <%= order.province %><%= order.city %>******</p>
                <p style="font-family: '宋體';font-size: 8pt;line-height: 16pt;">運單號: <%= package.shipping_code %></p>
                <p style="font-family: '宋體';font-size: 8pt;line-height: 16pt;">買家留言: <%= order.leave_message %></p>
              </div>
              <div style="width: 35%;position: absolute;right: 0;">
                <div style="width: 100pt;height: 100pt;">
                  <img src="<%= qrcode_file_path %>" style="width: 100%;height: 100%;">
                  <p style="font-family: '宋體';font-size: 8pt;line-height: 8pt;width: 100%;text-align: center;margin-top:0pt;">(掃碼確認發貨)</p>
                </div>
              </div>
            </div>
          </div>
          <div style="line-height: 16pt; margin-top: 20pt">
            <div>
              <p style="font-family: '宋體';font-size: 12pt;">商品信息</p>
            </div>
            <div style="margin-top: 10pt;">
              <% partner_order_items.each do |poi| %>
                <p style="font-family: '宋體';font-size: 8pt;line-height: 16pt;">品牌: <%= poi.product_brand %></p>
                <p style="font-family: '宋體';font-size: 8pt;line-height: 16pt;">名稱: <%= poi.product_name %></p>
                <p style="font-family: '宋體';font-size: 8pt;line-height: 16pt;">SKU: <%= poi.product_sku %></p>
                <p style="font-family: '宋體';font-size: 8pt;line-height: 16pt;">顏色: <%= poi.product_color %></p>
                <p style="font-family: '宋體';font-size: 8pt;line-height: 16pt;">尺碼: <%= poi.size.try(:value) %></p>
                <p style="font-family: '宋體';font-size: 8pt;line-height: 16pt;">商品售出價格:<%= poi.product_price.convert_price %></p>
              <% end %>
            </div>
          </div>
        </div>
        <% if order.is_vip_order %>
          <div style="position: absolute; bottom:20pt; width: 60pt;text-align: center;">
            <p style="font-family: '宋體'; font-weight: bold; line-height: 14pt; font-size: 8pt; background-color: black; color: white;padding: 5pt 0pt;">大客戶訂單</p>
          </div>
        <% end %>
      </div>
    </div>
  </body>
</html>

demo
在這裏插入圖片描述

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章