console
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<style>
#app {
padding-left: 300px;
}
* {
margin: 0;
padding: 0;
}
h1 {
color: #007bff;
}
p {
margin-bottom: 10px;
}
ul {
margin-bottom: 20px;
}
img {
max-width: 100%;
height: auto;
margin-bottom: 20px;
}
a {
color: #007bff;
text-decoration: none;
}
</style>
</head>
<body>
<button id="export">sdfsasdfds</button>
<div id="app">
<h1>Welcome to Sample HTML Page</h1>
<p>This is a simple HTML page with various content elements:</p>
<ul>
<li>Headings: h1, h2, h3, etc.</li>
<li>Paragraphs: p</li>
<li>Lists: ul, ol, li</li>
<li>Images: img</li>
<li>Links: a</li>
</ul>
<h2>Heading 2</h2>
<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nullam vitae eros nec eros venenatis fermentum. Proin
lacinia massa non velit elementum, eu fringilla eros sagittis.</p>
<h3>Heading 3</h3>
<p>Suspendisse nec tortor eget tortor varius fermentum nec vel tortor. Fusce nec magna id dui laoreet interdum. Ut
vel metus sagittis, fringilla libero in, bibendum est.</p>
<h2>Ordered List</h2>
<ol>
<li>Item 1</li>
<li>Item 2</li>
<li>Item 3</li>
</ol>
<h2>Unordered List</h2>
<ul>
<li>Item A</li>
<li>Item B</li>
<li>Item C</li>
</ul>
<h2>Image</h2>
<h2>Link</h2>
<p>Visit our website:
<a href="https://www.example.com" target="_blank">www.example.com</a>
</p>
</div>
<script src="https://html2canvas.hertzen.com/dist/html2canvas.js"></script>
<script>
document.querySelector("#export").addEventListener("click", () => {
const scale = window.devicePixelRatio || 1;
html2canvas(document.querySelector("#app"), {
allowTaint: true,
useCORS: true,
scale,
backgroundColor: "#fff",
logging: true,
height: 2000
}).then((canvas) => {
const ctx = canvas.getContext("2d");
const mergeCanvas = createCanvas(canvas.width, canvas.height)
const ctx2 = mergeCanvas.getContext('2d');
ctx2.drawImage(canvas, 0, 0, canvas.width, canvas.height);
// 合并下载二维码
createImg((myImage) => {
ctx2.drawImage(myImage, 0, 0);
mergeCanvas.toBlob((blob) => {
// download(blob)
appendBody(blob)
});
})
});
function download(blob) {
let url = URL.createObjectURL(blob);
let a = document.createElement("a");
a.href = url;
a.download = "个性化手册.png";
a.click();
}
function createImg(callBack) {
var myImage = new Image();
myImage.src = "./qr.png";
myImage.onload = function () {
callBack(myImage)
};
}
function appendBody(blob) {
let url = URL.createObjectURL(blob);
const img = document.createElement("img");
img.src = url;
document.body.appendChild(img);
}
function createCanvas(width, height) {
const canvas = document.createElement("canvas");
canvas.width = width;
canvas.height = height;
return canvas;
}
});
</script>
</body>
</html>