javascript - 使用 CSS 样式将当前页面保存为 PDF

标签 javascript jquery html css pdf

我有这个简单的代码可以保存我的当前页面(基于我希望保存的 ID),但是当它打开保存窗口时,我将其视为没有样式的纯 HTML 代码。有没有一种方法可以保存 PDF,包括来自 CSS 的所有样式?

<script>

function printDiv(divName) {

 var printContents = document.getElementById(divName).innerHTML;
 w=window.open();
 w.document.write(printContents);
 w.print();
 w.close();
}
</script>

最佳答案

function printDiv(divName) {
var printContents = document.getElementById(divName).innerHTML;
w=window.open();
w.document.write('<style type="text/css"> /* your code here */ </style>' + printContents);
w.print();
w.close();
}

关于javascript - 使用 CSS 样式将当前页面保存为 PDF,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42996064/

相关文章:

javascript - 使用 Google API 匹配城市和州?

javascript - bxslider 滑动对齐中心

JavaScript - 纸牌内存游戏,洗牌功能

javascript - 使用 Javascript/Jquery 的本地存储(不使用 HTML5)

javascript - 在每次迭代中附加到 dom 或创建一个数组并输出?

php - 如何调整图像大小保持约束php

javascript - 将 DOMContentLoaded 监听器附加到动态创建的窗口

javascript - knex js - 一对多关系

javascript - js.mapit 不工作

javascript - 根据 innerHTML 显示/隐藏 HTML 元素