score:3
This script allows you to take "screenshots" of webpages or parts of it, directly on the users browser. The screenshot is based on the DOM and as such may not be 100% accurate to the real representation as it does not make an actual screenshot, but builds the screenshot based on the information available on the page.
A HTML5 client-side solution for generating PDFs. Perfect for event tickets, reports, certificates, you name it!
FileSaver.js implements the saveAs() FileSaver interface in browsers that do not natively support it. There is a FileSaver.js demo that demonstrates saving various media types.
Put it all together and use jQuery to bind your actions. This will generate a PDF that can either be saved, printed, or viewed.
<script src="/path/to/jquery.min.js"></script>
<script src="/path/to/html2canvas.js"></script>
<script src="/path/to/jspdf.min.js"></script>
<script src="/path/to/FileSaver.min.js"></script>
<script>
$('#btnPrint').on('click', function(event) {
event.preventDefault();
html2canvas($('#printThis'), {
onrendered: function(canvas) {
var imgData = canvas.toDataURL('image/jpeg');
var doc = new jsPDF('landscape');
doc.addImage(imgData, 'JPEG', 15, 45, 270, 125);
doc.save('download.pdf');
return false;
}
});
});
</script>
For just print only using html2canvas:
<script src="/path/to/jquery.min.js"></script>
<script src="/path/to/html2canvas.js"></script>
<script>
$('#btnPrint').on('click', function(event) {
event.preventDefault();
html2canvas($('#printThis'), {
onrendered: function(canvas) {
var imgData = canvas.toDataURL('image/jpeg');
var windowContent = '<!DOCTYPE html>';
windowContent += '<html>'
windowContent += '<head><title>Print canvas</title></head>';
windowContent += '<body>'
windowContent += '<img src="' + imgData + '">';
windowContent += '</body>';
windowContent += '</html>';
var printWin = window.open('', '', 'width=340,height=260');
printWin.document.open();
printWin.document.write(windowContent);
printWin.document.close();
printWin.focus();
printWin.print();
printWin.close();
return false;
}
});
});
</script>
You could do this without the use of jQuery or any plugins, but you'll have to target the canvas directly without any HTML using just the HTMLCanvasElement.toDataURL() method:
<script>
document.getElementById('btnPrint').onclick = function () {
var imgData = document.getElementById('graficaMedidas').toDataURL('image/jpeg');
var windowContent = '<!DOCTYPE html>';
windowContent += '<html>'
windowContent += '<head><title>Print canvas</title></head>';
windowContent += '<body>'
windowContent += '<img src="' + imgData + '">';
windowContent += '</body>';
windowContent += '</html>';
var printWin = window.open('', '', 'width=340,height=260');
printWin.document.open();
printWin.document.write(windowContent);
printWin.document.close();
printWin.focus();
printWin.print();
printWin.close();
}
</script>
Source: stackoverflow.com
Related Query
- How to print a chart rendered by code
- How can I get my Chart.JS bar chart to stack two data values together on each bar, and print a calculated value on each bar?
- How do I destroy/update Chart Data in this chart.js code example?
- How to run Chart.js samples using source code
- How to get a value from function javascript and print it to the chart
- how to not repeat code while creating multiple charts in chart js
- How to add text inside the doughnut chart using Chart.js?
- How to clear a chart from a canvas so that hover events cannot be triggered?
- Chart.js - How to set a line chart dataset as disabled on load
- chart js 2 how to set bar width
- How can labels/legends be added for all chart types in chart.js (chartjs.org)?
- How can I create a horizontal scrolling Chart.js line chart with a locked y axis?
- How can I make two of my lines in Chart JS thicker
- How to prevent first/last bars from being cut off in a chart with time scale
- How set color family to pie chart in chart.js
- Chart.js Bar Chart - how to chart bars from 0
- How to add an on click event to my Line chart using Chart.js
- Chart.js how to show cursor pointer for labels & legends in line chart
- How do you hide the title of a chart tooltip?
- How to put rounded corners on a Chart.js Bar chart
- chartjs : how to set custom scale in bar chart
- How can I make a stepline or stepped chart in chart.js or D3?
- How to save Chart JS charts as image without black background using blobs and filesaver?
- How to Draw Gantt chart using chart js or other libraries
- How to fix chart Legends width-height with overflow scroll in ChartJS
- How to display Line Chart dataset point labels with Chart.js?
- chart js how to fill legend box with colour
- How to add an offset to a dataset in Chart js
- How to add second Y-axis for Bar and Line chart in Chart.js?
- How to add text in centre of the doughnut chart using Chart.js?
More Query from same tag
- Unable to make the x-zoom to work when xAxes type is different from 'time'
- ChartJS Custom text on certain xAxis and yAxis linesS
- ChartJS - Injecting data from the server-side
- chartjs radar glowing edges effect
- Chart.js - show tooltip when hovering on legend
- Display totals for Label (or simply display a string) inside a chart in Chart.js
- Order and hide items of legend by value with Chartjs Angular
- chartkick chart.js change colour of axis and colour of axis title
- Is it possible to make points in a line chart of Chart JS 3.7.0 look like a donut?
- How to shorten Chart.js label
- Can we use EJS tags with ChartsJS?
- Render Chart.js on backend side ASP.NET
- ChartJS padding from lines left and right
- ChartJS 2.0 differences in code help needed
- Script error : "Uncaught SyntaxError: Unexpected identifier"
- Chart.js : how I can adjust Pie chart radius?
- How to change bar color on a angular-chart and chart js bar graph with multiple datasets?
- How I can change the font-family of labels in the bar chart?
- Chart.JS Not Formatting Y-Axis-1 Properly
- How can i hide label data on chart.js tooltip content?
- pass array variable to chart.js options
- Chartkick-vue bar chart - "horizontalBar" is not a registered controller
- Equal distance grouped bar chart irrespective of scale - chart.js
- generating a Chart.js chart with python data
- ChartJS: How to get labels, legend, title to show up?
- Allow display of multiple data attributes in tooltip of Chart.js
- Run the function on load
- How to skip labels on x-axes?
- Make a Histogram in Chart.js
- Chart js 2 bars with one customize label on top