试试这个,也许能解决你的问题。
const downloadPdf = () => {
// Create a new <style> element and append it to the document's <head>
const style = document.createElement('style');
document.head.appendChild(style);
// Insert a CSS rule to ensure images are displayed inline-block
style.sheet?.insertRule('body > div:last-child img { display: inline-block; }');`enter code here`
// Use html2canvas to capture the desired HTML element
html2canvas(element).then(canvas => {
// Remove the temporary style element to clean up the DOM
style.remove();
// Continue with your PDF generation logic here
// For example, you could convert the canvas to a PDF using jsPDF
});
};
CSS 规则 body > div:last-child img { display: inline-block;确保指定容器内的图像显示为内联块元素。这有助于保持正确的布局并防止画布捕获过程中出现渲染问题。