简体   繁体   English

canvg是未定义的错误

[英]canvg is undefined error

I am trying to use the code from Battlehorse to convert A Google Visualization chart to an image to save to a server. 我正在尝试使用来自Battlehorse的代码将Google可视化图表转换为图像以保存到服务器。 I was able to get this to work on localhost but when I attempt to use it on the Web Server I get the error "canvg is undefined". 我能够在localhost上使用它,但当我尝试在Web服务器上使用它时,我收到错误“canvg is undefined”。 The Web Server is running IIS 7. I have searched quite a bit and have not been able to find any information regarding this error. Web服务器正在运行IIS 7.我搜索了很多,但无法找到有关此错误的任何信息。 Does anyone know what causes this error or how to resolve it? 有谁知道导致此错误的原因或解决方法?

http://www.battlehorse.net/page/topics/charts/save_google_charts_as_image.html http://www.battlehorse.net/page/topics/charts/save_google_charts_as_image.html

在此输入图像描述

Code Sample: 代码示例:

<html>
<head>
<script type="text/javascript" src="http://canvg.googlecode.com/svn/trunk/rgbcolor.js"></script> 
<script type="text/javascript" src="http://canvg.googlecode.com/svn/trunk/canvg.js"></script>
<script type="text/javascript">
function getImgData(chartContainer) {
  var chartArea = chartContainer.getElementsByTagName('svg')[0].parentNode;
  var svg = chartArea.innerHTML;
  var doc = chartContainer.ownerDocument;
  var canvas = doc.createElement('canvas');
  canvas.setAttribute('width', chartArea.offsetWidth);
  canvas.setAttribute('height', chartArea.offsetHeight);

  canvas.setAttribute(
  'style',
  'position: absolute; ' +
  'top: ' + (-chartArea.offsetHeight * 2) + 'px;' +
  'left: ' + (-chartArea.offsetWidth * 2) + 'px;');
  doc.body.appendChild(canvas);
  canvg(canvas, svg);
  var imgData = canvas.toDataURL("image/png");
  canvas.parentNode.removeChild(canvas);
  return imgData;}           

function alert10() {
  try {
    var textbox1 = document.getElementById('textbox1');
    textbox1.value = getImgData(document.getElementById('chart1_div'));           
  }
  catch (err) {
    alert(err.message);
  }
}
</script>
</head>

I figured it out. 我想到了。 The problem was that the site was running with SSL enabled and I was calling external script files by http protocols. 问题是该网站在启用SSL的情况下运行,我通过http协议调用外部脚本文件。 I had to adjust the external script file references to either use https OR change them to relative like this: 我必须调整外部脚本文件引用以使用https或将它们更改为相对像这样:

<script type="text/javascript" src="//canvg.googlecode.com/svn/trunk/rgbcolor.js"></script> 
<script type="text/javascript" src="//canvg.googlecode.com/svn/trunk/canvg.js"></script>

Using the protocol relative path like "//..." makes sure it will work in both HTTP and HTTPS 使用协议相对路径(如“// ...”)可确保它在HTTP和HTTPS中都能正常工作

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM