Image not rendering on PDF using jsPDF? - html

I have included the following files:
<script type="text/javascript" src="libs/png_support/zlib.js"></script>
<script type="text/javascript" src="libs/png_support/png.js"></script>
<script type="text/javascript" src="jspdf.plugin.addimage.js"></script>
<script type="text/javascript" src="jspdf.plugin.png_support.js"></script>
<script type="text/javascript" src="jspdf/jspdf.plugin.standard_fonts_metrics.js"></script>
<script type="text/javascript" src="jspdf/jspdf.plugin.split_text_to_size.js"></script>
<script type="text/javascript" src="jspdf/jspdf.plugin.from_html.js"></script>
<script type="text/javascript" src="jspdf.js"></script>
And I am just testing if it could render a image through html:
var doc = new jsPDF();
var elementHandler = {
'#ignorePDF': function (element, renderer) {
return true;
}
};
var source = '<img src="/assets/common/image/BG.jpg"/>';
doc.fromHTML(
source,
15,
15,
{
'width': 180,'elementHandlers': elementHandler
});
doc.output("dataurlnewwindow");
It throws this error on console which says:
jsPDF Warning: rendering issues? provide a callback to fromHTML! (anonymous function)
I have used PNG format, since it was not working I also tried with JPG format, still no luck!
What am I doing wrong?
Thanks in advance.

Have one more argument for fromHTML()
doc.fromHTML(
source,
15,
15, {
'width': 180,
'elementHandlers': elementHandler
},
function(dispose) {
// dispose: object with X, Y of the last line add to the PDF
// this allow the insertion of new lines after html
// pdf.save('Test.pdf');
if (navigator.msSaveBlob) {
var string = doc.output('datauristring');
} else {
var string = doc.output('bloburi');
}
$('.previewIFRAME').attr('src', string);
})

Related

Not getting data by using ngDrive module by pinoyyid

<html>
<body ng-app="MyApp">
<div ng-controller="MainCtrl as vm">
<li ng-repeat="file in vm.filesArray">{{file.title}}</li>
</div>
<script src="https://apis.google.com/js/auth.js" type="text/javascript"></script>
<script src="../bower_components/angular/angular.min.js"></script>
<script src="../bower_components/ngDrive/build/module.js"></script>
<script>
var myApp = angular.module('MyApp', ['ngm.ngDrive']);
angular.module('ngm.ngDrive')
.provider('OauthService', ngDrive.Config)
.config(function (OauthServiceProvider) {
OauthServiceProvider.setScopes('https://www.googleapis.com/auth/drive.file');
OauthServiceProvider.setClientID('10503878599-98uqrqmgq2f3kc1sbr13l6rma20ue7l0.apps.googleusercontent.com');
});
var MainCtrl = (function () {
function MainCtrl(DriveService) {
this.filesArray = DriveService.files.list({
q: 'trashed = false',
maxResults: 10,
fields: 'items/title'
}, true).data;
}
MainCtrl.$inject = ['DriveService'];
return MainCtrl;
})();
angular.module('MyApp').controller('MainCtrl', MainCtrl);
</script>
</body>
</html>
I am using ngDrive by pinoyyid .
This is my index.html .
I have provided a valid client ID.
It returns an empty array in files array.
Please suggest any solution

Query CSV file with google visualization query

I am trying to query a csv file with google visualization query but every time I am getting 'Error in query: 404 file not found' error and am not able to resolve it.
any pointers / suggestions / thought would be highly appreciated.
Note - the csv file exists on my local server and if click on link http://localhost:35802/test.csv it downloaded the file for me
here is my code --
<html>
<head>
<script type="text/javascript" src="https://www.gstatic.com/charts/loader.js"></script>
<script>
function initialize() {
//var opts = { sendMethod: 'auto' };
// Replace the data source URL on next line with your data source URL.
var query = new google.visualization.Query('csv?url=http://localhost:35802/test.csv');
// Optional request to return only column C and the sum of column B, grouped by C members.
query.setQuery('select * ');
// Send the query with a callback function.
query.send(handleQueryResponse);
}
function handleQueryResponse(response)
{
if (response.isError())
{
alert('Error in query: ' + response.getMessage() + ' ' + response.getDetailedMessage());
return;
}
//alert(response);
var data = response.getDataTable();
var chart = new google.visualization.ColumnChart(document.getElementById('chart_div'));
chart.draw(data, { height: 400 });
}
</script>
<script type="text/javascript">
google.charts.load('current', {packages: ['corechart']});
google.charts.setOnLoadCallback(initialize);
</script>
</head>
<body>
<div id="chart_div"></div>
</body>
here is my csv file content --
A,B,C
1,2,3
4,5,6
Thanks a lot #WhiteHat for the pointers.. finally I got it working...:)
the culprit was the google library that I was loading.
Initially I was loading --
<script type="text/javascript" src="https://www.gstatic.com/charts/loader.js"></script>
which was not working for me...
I was able to run my code by changing this library to
<script type="text/javascript" src="https://www.google.com/jsapi"></script>
for anyone who needs a working sample here it is --
please change the path to csv file
<html>
<head>
<script type="text/javascript" src="https://www.google.com/jsapi"></script>
<script type="text/javascript">
google.load("visualization", "1", { packages: ["corechart"] });
google.setOnLoadCallback(drawChart);
function drawChart() {
// var query = new google.visualization.Query('http://www.gstatic.com/external_hosted/gstatic/charts/static/data.csv',
// { csvColumns: ['string', 'number'], csvHasHeader: true });
var opt = { sendMethod: 'xhr' }; //{ csvColumns: ['string', 'number'], csvHasHeader: true, sendMethod: auto });
//var query = new google.visualization.Query('http://local:6572/data.csv', opt);
var query = new google.visualization.Query('http://localhost:6572/data.csv');
query.setQuery('select *');
query.send(handleQueryResponse);
}
function handleQueryResponse(response) {
if (response.isError()) {
alert('Error in query: ' + response.getMessage() + ' ' +
response.getDetailedMessage());
return;
}
var data = response.getDataTable();
var chart = new google.visualization.ColumnChart(document.getElementById('csv'));
chart.draw(data, { legend: { position: 'none'} });
}
</script>
</head>
<body>
<div id="csv" style="width: 900px; height: 500px;"></div>
</body>
</html>

google map error TypeError: h[b] is undefined

I am trying to simply test the geolocation example, my code
<script type="text/javascript" src="http://maps.google.com/maps/api/js?v=3&sensor=false"></script>
<script type="text/javascript" src="includes/js/map/jquery.ui.map.min.js"></script>
<script type="text/javascript" src="includes/js/map/jquery.ui.map.overlays.min.js"></script>
<script>
$(function () {
$('#user_location').gmap().bind('init', function(evt, map) {
$('#user_location').gmap('getCurrentPosition', function(position, status) {
if ( status === 'OK' ) {
var clientPosition = new google.maps.LatLng(position.coords.latitude, position.coords.longitude);
$('#user_location').gmap('addMarker', {'position': clientPosition, 'bounds': true});
$('#user_location').gmap('addShape', 'Circle', {
'strokeWeight': 0,
'fillColor': "#008595",
'fillOpacity': 0.25,
'center': clientPosition,
'radius': 15,
'clickable': false
});
}
});
});
});
</script>
But when I try to run this, I get TypeError: h[b] is undefined
Also on a sidenote, is it possible to store a reference to this marker so that I can display message, or remove it later?
If you come across the same problem, try to:
replace minified version of map plugin to full (it could help you understand the problem)
make sure you have linked all the neccessary javascript files
In my case, I have missed to link this two libraries (just compare to what libraries are linked in the example file):
<script type="text/javascript" src="/js/jquery.ui.map.overlays.js"></script>
<script type="text/javascript" src="/js/jquery.ui.map.extensions.js"></script>

Real time rendering using JSPlot

I have started using JQPlot to draw the graph in real time as well. I have got one sample example working in which it will draw the graph with two data only-
var storedData = [70, 10];
Then I have added a button, if I click on that button, it will start rendering some more data. Basically, you can say just like real time data from the server-
But the problem that I am facing currently is, as soon as I clicked on Start Updates button, it doesn't draw anything. So I believe, I messed up something the button click thing. It's been a long time I worked on Javascript and HTML thing so I have forgotten lot of things.
Below is my code-
<!DOCTYPE html>
<html>
<head>
<title>Line Charts and Options</title>
<link class="include" rel="stylesheet" type="text/css" href="../jquery.jqplot.min.css" />
<link rel="stylesheet" type="text/css" href="examples.min.css" />
<link type="text/css" rel="stylesheet" href="syntaxhighlighter/styles/shCoreDefault.min.css" />
<link type="text/css" rel="stylesheet" href="syntaxhighlighter/styles/shThemejqPlot.min.css" />
<!--[if lt IE 9]><script language="javascript" type="text/javascript" src="../excanvas.js"></script><![endif]-->
<script class="include" type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
</head>
<body>
<div class="colmask leftmenu">
<div class="colleft">
<div class="col1" id="example-content">
<div id="chart1" style="height: 300px; width: 500px; position: relative;"></div>
<button>Start Updates</button>
<script class="code" type="text/javascript">
$(document).ready(function(){
var storedData = [70, 10];
var plot1;
renderGraph();
$('button').click( function(){
doUpdate();
$(this).hide();
});
function renderGraph() {
if (plot1) {
plot1.destroy();
}
plot1 = $.jqplot('chart1', [storedData]);
}
var newData = [9, 1, 4, 6, 8, 2, 5];
function doUpdate() {
if (newData.length) {
var val = newData.shift();
$.post('/echo/html/', {
html: val
}, function(response) {
var newVal = new Number(response); /* update storedData array*/
storedData.push(newVal);
renderGraph();
log('New Value '+ newVal+' added')
setTimeout(doUpdate, 3000)
})
} else {
log("All Done")
}
}
function log(msg) {
$('body').append('<div>'+msg+'</div>')
}
});
</script>
<script class="include" type="text/javascript" src="../jquery.jqplot.min.js"></script>
<script type="text/javascript" src="syntaxhighlighter/scripts/shCore.min.js"></script>
<script type="text/javascript" src="syntaxhighlighter/scripts/shBrushJScript.min.js"></script>
<script type="text/javascript" src="syntaxhighlighter/scripts/shBrushXml.min.js"></script>
<script class="include" type="text/javascript" src="../plugins/jqplot.canvasTextRenderer.min.js"></script>
<script class="include" type="text/javascript" src="../plugins/jqplot.canvasAxisLabelRenderer.min.js"></script>
</body>
</html>
I am not able to make the button click event work. As soon as I click on Start Update button, it does not draw anything. Correct behavior should be that it should start rendering on the graph with new datasets.
I tried to make that real-time rendering thing work from this jsfiddle
I copied same bunch of code from the above JSFiddle in my HTML but it doesn't working for me. Can anybody help me with this?
Here you go - you will have to update to your jqplot location in script tags. Also, I had to change the doUpdate to doUpdate2 as $.post will not work without jfiddle as far as I know.
<!DOCTYPE html>
<html>
<head>
<title>Line Charts and Options</title>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<script type="text/javascript" src="jqplot/jquery.jqplot.min.js"></script>
<script type="text/javascript" src="jqplot/plugins/jqplot.canvasAxisTickRenderer.min.js"></script>
<script type="text/javascript" src="jqplot/plugins/jqplot.canvasTextRenderer.min.js"></script>
<script type="text/javascript" src="jqplot/plugins/jqplot.canvasAxisLabelRenderer.min.js"></script>
<script type="text/javascript" src="jqplot/plugins/jqplot.cursor.min.js"></script>
<script type="text/javascript" src="jqplot/plugins/jqplot.pointLabels.min.js"></script>
<script type="text/javascript" src="jqplot/plugins/jqplot.highlighter.min.js"></script>
<script type="text/javascript">
$(function(){
/* store empty array or array of original data to plot on page load */
var storedData = [70, 10];
/* initialize plot*/
var plot1;
renderGraph();
$('button').click( function(){
doUpdate2();
$(this).hide();
});
function renderGraph() {
if (plot1) {
plot1.destroy();
}
plot1 = $.jqplot('chart1', [storedData]);
}
var newData = [9, 1, 4, 6, 8, 2, 5];
var cp = 0;
function doUpdate2() {
var newVal = new Number(newData[cp]); /* update storedData array*/
storedData.push(newVal);
renderGraph();
log('New Value '+ newVal+' added')
if (cp < newData.length-1) setTimeout(doUpdate2, 3000)
cp++;
}
function doUpdate() {
if (newData.length) {
var val = newData.shift();
$.post('/echo/html/', {
html: val
}, function(response) {
var newVal = new Number(response); /* update storedData array*/
storedData.push(newVal);
renderGraph();
log('New Value '+ newVal+' added')
setTimeout(doUpdate, 3000)
})
} else {
log("All Done")
}
}
function log(msg) {
$('body').append('<div>'+msg+'</div>')
}
});
</script>
</head>
<body>
<div id="chart1" style="height: 300px; width: 500px; position: relative;"></div>
<button>Start Updates</button>
</body>
</html>

Loading Google Chart Api using Ajax in html page

I am using Ajax code to load the html page
for example:
$.ajax({
url: 'Context.html',
dataType: 'html',
timeout: 500,
success: function(html) {
$("div#mainbody").html(html);
}
});
The Context.html I am loading it in some other html page say Home.html
But I am generating pie charts using google API in Context.html
and the code for generating pie chart i.e in Context.html is
<script type="text/javascript" src="https://www.google.com/jsapi"></script>
<script type="text/javascript">
google.load("visualization", "1", {packages:["corechart"]});
google.setOnLoadCallback(drawChart);
function drawChart() {
var data = google.visualization.arrayToDataTable([
['Year', 'Count'],
['2005', 70],
['2006', 80],
['2007', 140],
['2008', 220],
['2009', 290],
['2010', 400],
['2011', 500]
]);
var options = {
title: 'Head Count-(Apple Year)',
colors:['#129212']
};
var chart = new google.visualization.ColumnChart(document.getElementById('jsf_headcount_bargraph'));
chart.draw(data, options);
}
</script>
When I am loading Context.html in Home.html page I cannot find the pie chart which is in Context.html after loading it in the Home.html
I tried by giving ALERT(""); in script where I wrote code for pie chart. I was getting alert message,so Ajax is executing javascript but I am not getting pie chart which is same script. So I was stucked with Loading pie chart in Home.html page
If you make Context.html like this
<script>
var j = 20;
alert();
</script>
Then I do get an alert (using $.ajax({success: function() {} }) etc. ).
[ Example ]
function execAjax() {
$.ajax( {
url: 'index2.html',
success: function( html ) {
$("#content").html( html );
checkJSvalue();
},
error: function() {
alert( "Error" );
}
});
}
function checkJSvalue() {
alert( j );
}
Example HTML (index2.html)
<div>
content of index2.html
</div>
<script>
var j = 20;
alert();
</script>
What I have tried to do is to put the code directly in the 'home.html' and I already got errors. You should reverse the order of usage and declaration of the drawchart function anyways.
// Bad
// google.setOnLoadCallback(drawChart);
// function drawChart() {}
// Good
function drawChart() {}
google.setOnLoadCallback(drawChart);
Other than that, I already got this error from the google.jsapi.js (copied locally)
Uncaught Error: Container is not defined format+nl,default,corechart.I.js:841
So something goes wrong there, that is not part of the actual question and it's minified etc. so I won't go there.
Hope this was helpful :)
Everything works just fine if i replace the code
google.load("visualization", "1", {packages:["corechart"]});
google.setOnLoadCallback(drawChart);
To inline
google.load('visualization', '1.0', {'packages':['corechart'], "callback": drawChart});