我正在开发一个从Ruby on Rails 4中的Express Carriers获取货运标签的应用程序。此刻,我正在使用UPS API及其标签(即GIF图像)。 为了能够生成包含GIF标签的PDF,我使用了不支持GIF的Prawn。因此,GIF转换为PNG。我可以贴标签,但是当我打印出来时,质量很低,而且很难看清标签内的内容。 我需要了解如何添加更好的质量,否则标签将无法使用。
我正在上的课:
class UPSLabelConsolidation
attr_reader :base64_encoded_gif_labels
def initialize(base64_encoded_gif_labels)
@base64_encoded_gif_labels = base64_encoded_gif_labels
end
def perform!
identifier = SecureRandom.hex(3)
tmp_label_files = base64_encoded_gif_labels.each_with_index.map do |base64_encoded_gif_label, index|
tmp_label_file = Tempfile.new(["ups-label-#{identifier}-#{index}", ".png"], Rails.root.join("tmp"))
tmp_label_file.binmode
image = MiniMagick::Image.read(Base64.decode64(base64_encoded_gif_label), "gif")
image.combine_options do |b|
b.rotate "90" # Rotate 90-degrees clockwise
b.crop "800x1000+0+0" # Crop ~200px whitespace from the bottom of the label
end
image.format("png") # Prawn does not support GIF, so we convert it here.
image.write(tmp_label_file.path)
tmp_label_file.rewind
tmp_label_file
end
new_page = false
@pdf = Prawn::Document.new(page_size: [297, 390], margin: 20, page_layout: :portrait) # Margin is ~0.7cm
tmp_label_files.each do |label_file|
@pdf.start_new_page if new_page
@pdf.image label_file, width: 250, position: :left # Width is ~8.8cm
new_page = true
end
end
def write!(path)
if @pdf
@pdf.render_file(path)
else
raise "`#perform!` before `#write!`"
end
end
end