Skip to content

Instantly share code, notes, and snippets.

@rparree
Created November 28, 2017 14:25
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rparree/3d842494e1ebeef041b1e9423921e013 to your computer and use it in GitHub Desktop.
Save rparree/3d842494e1ebeef041b1e9423921e013 to your computer and use it in GitHub Desktop.
// with some inspiration from https://jsfiddle.net/uggmnho5/4/
const base = url.substr(0,url.lastIndexOf("/")) + "/";
$.get(url).then((htmlText) => {
htmlText = htmlText.replace(/src=("|\')(?!https?:|\/)/g, 'src=$1' + base) // expand to to other needs
const myDocURL = URL.createObjectURL(new Blob([htmlText], {
type: 'text/html'
}));
takeSnapshotOfURL(myDocURL,div.dataset)
.then((canvas) => {
div.parentNode.replaceChild(canvas,div);
});
})
function takeSnapshotOfURL(url,options) {
const iframe = document.createElement('iframe');
iframe.src = url;
iframe.style.cssText = 'position: absolute; opacity:0; z-index: -9999';
document.body.appendChild(iframe);
return new Promise(function (res, rej) {
iframe.onload = function (e) {
const html2canvasOptions = {
logging: true,
background:"white",
};
html2canvas(iframe.contentDocument.documentElement, html2canvasOptions)
.then(function (canvas) {
document.body.removeChild(iframe);
res(canvas);
})
.catch(rej);
};
iframe.onerror = rej;
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment