如何用Ruby准确获取PDF文档每页的具体尺寸?
- 内容介绍
- 文章标签
- 相关推荐
本文共计180个文字,预计阅读时间需要1分钟。
是否有一个好库可以确定PDF页面的维度?我知道一种方法是使用rghost gem将PDF转换为png,然后使用image_size gem读取png维度。我不喜欢这种方法。pdf-reader gem可以做到这一点。+require 'pdf/rea'
是否有一个好的库可以确定PDF页面的维度?我知道一种方法是使用rghost gem将pdf转换为png然后使用image_size gem来读取png维度.我不喜欢这种方法.
pdf-reader gem可以做到这一点.require 'pdf/reader' require 'bigdecimal' def pt2mm(pt) (pt2in(pt) * BigDecimal.new("25.4")).round(2) end def pt2in(pt) (pt / BigDecimal.new("72")).round(2) end reader = PDF::Reader.new("somefile.pdf") reader.pages.each do |page| bbox = page.attributes[:MediaBox] width = bbox[2] - bbox[0] height = bbox[3] - bbox[1] puts "width: #{width}pts #{pt2mm(width).to_s("F")}mm #{pt2in(width).to_s("F")}in" puts "height: #{height}pts #{pt2mm(height).to_s("F")}mm #{pt2in(height).to_s("F")}in" end
本文共计180个文字,预计阅读时间需要1分钟。
是否有一个好库可以确定PDF页面的维度?我知道一种方法是使用rghost gem将PDF转换为png,然后使用image_size gem读取png维度。我不喜欢这种方法。pdf-reader gem可以做到这一点。+require 'pdf/rea'
是否有一个好的库可以确定PDF页面的维度?我知道一种方法是使用rghost gem将pdf转换为png然后使用image_size gem来读取png维度.我不喜欢这种方法.
pdf-reader gem可以做到这一点.require 'pdf/reader' require 'bigdecimal' def pt2mm(pt) (pt2in(pt) * BigDecimal.new("25.4")).round(2) end def pt2in(pt) (pt / BigDecimal.new("72")).round(2) end reader = PDF::Reader.new("somefile.pdf") reader.pages.each do |page| bbox = page.attributes[:MediaBox] width = bbox[2] - bbox[0] height = bbox[3] - bbox[1] puts "width: #{width}pts #{pt2mm(width).to_s("F")}mm #{pt2in(width).to_s("F")}in" puts "height: #{height}pts #{pt2mm(height).to_s("F")}mm #{pt2in(height).to_s("F")}in" end

