首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Phantomjs zoomFactor -缩放到特定大小的栅格化

Phantomjs zoomFactor -缩放到特定大小的栅格化
EN

Stack Overflow用户
提问于 2014-09-22 11:05:20
回答 1查看 2.4K关注 0票数 5

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

代码语言:javascript
复制
<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>

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

代码语言:javascript
复制
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

如您所见,单个可见元素的大小为1050px x 750px。我想将其精确地栅格化到10.5in x 7.5in的纸张大小上,大小为100%。

我的光栅化脚本做到了:

代码语言:javascript
复制
page.paperSize = width: '10.5in', height: '7.5in', border: '0in'

结果是像这样的pdf:

所以它不能扩展到全尺寸。我可以通过调整缩放因子来调整到全尺寸。在实验中,我发现这是可行的

代码语言:javascript
复制
page.zoomFactor = 1.991

现在元素可以正常缩放了,但是字体放大太多了。

如何在保持原始字体大小的同时,将页面最左边/最上面的1050px/750px缩放到纸上的10.5inx7.5英寸?

EN

回答 1

Stack Overflow用户

发布于 2015-03-24 01:05:37

你已经尝试过在css上进行媒体查询了吗?

代码语言:javascript
复制
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
代码语言:javascript
复制
@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;
}
代码语言:javascript
复制
<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>

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/25966175

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档