0
我使用RBPDF将json数据转换为pdf。 这里params[:image]
是json在客户端通过fabricjs(JSON.stringify()
)生成的数据。RBPDF fabric.js到pdf:旋转时不正确的结果
objects = JSON.parse(params[:image])
objectid = 0
defaultasX = 1.0
defaultasY = 1.0
@pdf = RBPDF.new()
@pdf.SetLeftMargin(10)
@pdf.SetRightMargin(10)
@pdf.set_header_margin(10)
@pdf.set_footer_margin(10)
@pdf.setPrintFooter(false)
@pdf.add_page()
objects["state"]["objects"].map { |object|
objectid = objectid + 1 ;
@pdf.StartTransform()
#fit to Screen
if(defaultasX == 1 && defaultasX == 1)
defaultasX = 200.0/(object["width"] * object["scaleX"])
defaultasY = 130.0/(object["height"] * object["scaleY"])
end
left = (object["left"]) * defaultasX
top = (object["top"]) * defaultasY
width = object["width"] * object["scaleX"] * defaultasX
height = object["height"] * object["scaleY"] * defaultasY
if object["type"] == 'i-text'
@pdf.set_xy(left,top)
@pdf.SetFontSize(5)
@pdf.MultiCell(width, height, object["text"])
puts "printing text at (" + left.to_s+ ' ' + top.to_s + ' ' + width.to_s+ ' ' + height.to_s + ')'
elsif object["type"] == 'image'
@pdf.set_xy(left, top)
@pdf.Rotate(360 - object["angle"])
dataURI = object["src"]
extension = dataURI.split(',')[0].split(':')[1].split(';')[0].split('/')[1]
byteString = dataURI.split(',')[1]
filename = "#{Rails.root}/public/uploads/tmpimg"+ objectid.to_s() + "." + extension
imagefiletmp = Base64.decode64(byteString)
File.open(filename, 'wb') do |f|
f.write(imagefiletmp)
end
@pdf.Image(filename , left, top, width, height)
@pdf.Rect(left-5, top-5, 10, 10)
end
@pdf.StopTransform()
}
@pdf.output('./public/uploads/abc.pdf','F')
所有事情做出正确的结果,但旋转宽度变长。
这是PDF图像部分
这是为什么发生?