Phantomjs zoomFactor - 将光栅化缩放到特定大小

Geo*_*uer 5 pdf svg rasterizing phantomjs

我正在光栅化以下简单的html + svg + foreignObject html

<html>
<head>
  <meta charset="UTF-8">
  <!--reset stylesheet -->
  <link rel="stylesheet" type="text/css" href="https://phantomjs.googlecode.com/git-history/cbdd80e98ea1eb29d5d3a9c65c84798b472b59b1/website/reset.css" />
  <style>
  p {
    border: 1px solid red;
    font-size: 15px !important;
  }
  svg {
    outline: 1px solid purple;
  }
  </style>
</head>
<body style="height: 1050px; width: 1050px; max-width: 1050px; max-height: 750px;">
    
<svg style="width: 1050px; height: 750px;">
  <foreignobject height="40" requiredfeatures="http://www.w3.org/TR/SVG11/feature#Extensibility" width="45" x="45" y="45">
    <body xmlns="http://www.w3.org/1999/xhtml">
      <p>Oh when the sun begins to shine.</p>
    </body>
  </foreignobject>
</svg>
</body>
</html>
Run Code Online (Sandbox Code Playgroud)

使用这个简单的光栅化脚本:

webPage = require 'webpage' 
args    = (require 'system').args        
url = args[1]
destination = args[2]

page  = webPage.create()

page.paperSize = width: '10.5in', height: '7.5in', border: '0in'
#page.zoomFactor = 1.991
#page.viewportSize = (width: 1050, height: 75)

page.open url, (status) ->
  if status isnt 'success'
    console.log "Error!", url, status
    return phantom.exit()

  rasterize = -> 
    page.render destination
    console.log "Rasterized", url, "to", destination
    return phantom.exit()
  setTimeout rasterize, 100
Run Code Online (Sandbox Code Playgroud)

Chrome显示svg

如您所见,单个可见元素的大小为1050px x 750px.我想将它精确地光栅化到尺寸为100%的10.5英寸x 7.5英寸纸张尺寸上.

我的光栅化脚本有:

page.paperSize = width: '10.5in', height: '7.5in', border: '0in'
Run Code Online (Sandbox Code Playgroud)

结果是这样的pdf:

缩放不正确

所以这不会扩展到全尺寸.我可以通过调整缩放系数来调整到全尺寸.实验上我发现这很有效

page.zoomFactor = 1.991
Run Code Online (Sandbox Code Playgroud)

适当缩放,字体不合适

现在元素正确缩放但字体缩放太多.

如何保持原始字体大小,将纸张的左/最顶部1050px/750px缩小到纸上10.5inx7.5in?

Rus*_*pez 0

您是否已经尝试过在 CSS 上使用媒体查询?

webPage = require 'webpage'
args = (require 'system').args
url = args[1]
destination = args[2]

page = webPage.create()

page.paperSize = width: '10.5in', height: '7.5in', border: '0in'#
page.zoomFactor = 1.991# page.viewportSize = (width: 1050, height: 75)

page.open url, (status) - >
  if status isnt 'success'
console.log "Error!", url, status
return phantom.exit()

rasterize = - >
  page.render destination
console.log "Rasterized", url, "to", destination
return phantom.exit()
setTimeout rasterize, 100
Run Code Online (Sandbox Code Playgroud)
@media print {
  body {
    margin: 0;
    background-image: none;
    height: 10.5in;
    width: 7.5in;
  }
}
p {
  border: 1px solid red;
  font-size: 8pt !important;
}
svg {
  outline: 1px solid purple;
}
Run Code Online (Sandbox Code Playgroud)
<link rel="stylesheet" type="text/css" href="https://phantomjs.googlecode.com/git-history/cbdd80e98ea1eb29d5d3a9c65c84798b472b59b1/website/reset.css" />

<body style="height: 1050px; width: 1050px; max-width: 1050px; max-height: 750px;">

  <svg style="width: 1050px; height: 750px;">
    <foreignobject height="40" requiredfeatures="http://www.w3.org/TR/SVG11/feature#Extensibility" width="45" x="45" y="45">

      <body xmlns="http://www.w3.org/1999/xhtml">
        <p>Oh when the sun begins to shine.</p>
      </body>
    </foreignobject>
  </svg>
</body>
Run Code Online (Sandbox Code Playgroud)