开发者社区> 问答> 正文

Cssbox ImageRenderer报错 

@superOrange 你好,想跟你请教个问题: ImageRenderer 有什么属性可以更改生成后的图片大小,好像样式不对

展开
收起
kun坤 2020-06-03 13:10:50 985 0
1 条回答
写回答
取消 提交回答
  • 我记得有个设置: ImageRenderer render = new ImageRenderer(); render.setWindowSize(new Dimension(1900, 1000), false); 部分源码: public void setWindowSize(Dimension size, boolean crop)     {         windowSize = new Dimension(size);         cropWindow = crop;     }   public boolean renderURL(String urlstring, OutputStream out, Type type) throws IOException, SAXException     {         if (!urlstring.startsWith("http:") &&             !urlstring.startsWith("https:") &&             !urlstring.startsWith("ftp:") &&             !urlstring.startsWith("file:"))                 urlstring = "http://" + urlstring;                  //Open the network connection          DocumentSource docSource = new DefaultDocumentSource(urlstring);                  //Parse the input document         DOMSource parser = new DefaultDOMSource(docSource);         Document doc = parser.parse();                  //create the media specification         MediaSpec media = new MediaSpec(mediaType);         media.setDimensions(windowSize.width, windowSize.height);         media.setDeviceDimensions(windowSize.width, windowSize.height);         //Create the CSS analyzer         DOMAnalyzer da = new DOMAnalyzer(doc, docSource.getURL());         da.setMediaSpec(media);         da.attributesToStyles(); //convert the HTML presentation attributes to inline styles         da.addStyleSheet(null, CSSNorm.stdStyleSheet(), DOMAnalyzer.Origin.AGENT); //use the standard style sheet         da.addStyleSheet(null, CSSNorm.userStyleSheet(), DOMAnalyzer.Origin.AGENT); //use the additional style sheet         da.addStyleSheet(null, CSSNorm.formsStyleSheet(), DOMAnalyzer.Origin.AGENT); //render form fields using css         da.getStyleSheets(); //load the author style sheets                  BrowserCanvas contentCanvas = new BrowserCanvas(da.getRoot(), da, docSource.getURL());         contentCanvas.setAutoMediaUpdate(false); //we have a correct media specification, do not update         contentCanvas.getConfig().setClipViewport(cropWindow);         contentCanvas.getConfig().setLoadImages(loadImages);         contentCanvas.getConfig().setLoadBackgroundImages(loadBackgroundImages);         if (type == Type.PNG)         {             contentCanvas.createLayout(windowSize);             ImageIO.write(contentCanvas.getImage(), "png", out);         }         else if (type == Type.SVG)         {             setDefaultFonts(contentCanvas.getConfig());             contentCanvas.createLayout(windowSize);             Writer w = new OutputStreamWriter(out, "utf-8");             writeSVG(contentCanvas.getViewport(), w);             w.close();         }                  docSource.close();         return true;     }

    2020-06-03 14:08:30
    赞同 展开评论 打赏
问答标签:
问答地址:
问答排行榜
最热
最新

相关电子书

更多
零基础CSS入门教程 立即下载
低代码开发师(初级)实战教程 立即下载
阿里巴巴DevOps 最佳实践手册 立即下载