简体   繁体   English

如何在不影响页面的情况下指导 html2pdf 使用哪个样式表?

[英]How can I direct html2pdf which stylesheet to use without affecting the page?

I'm using the library Html2Pdf which works really well to allow my app's users to download as PDF their invoices displayed in HTML.我正在使用库Html2Pdf ,它非常好地允许我的应用程序的用户下载 PDF 他们在 HTML 中显示的发票。 The problem encountered is when anyone needs to download his/her invoice while using dark mode.遇到的问题是任何人在使用暗模式时需要下载他/她的发票。

Switching from light to dark mode and reverse works by adding/removing a "disabled" attribute to the link tag:通过向链接标签添加/删除“禁用”属性来从浅色模式切换到深色模式和反向工作:

<link href="path/to/css/app.css" rel="stylesheet" type="text/css" id="light" />
<link href="path/to/css/app-dark.css" rel="stylesheet" type="text/css" id="dark" disabled="disabled" />

Both files have the same css rules, only colours change.两个文件具有相同的 css 规则,只有颜色改变。

If someone is using dark mode and need to download a PDF, html2pdf.js will print it exactly as it shown based on current css rules, therefore with black/gray backgrounds/fonts which isn't really ideal for an invoice!如果有人使用暗模式并需要下载 PDF,html2pdf.js 将根据当前的 css 规则将其完全打印出来,因此黑色/灰色背景/字体对于发票来说并不理想!

I already tried to change styles dynamically in the function which render the PDF after click, but of course the change is clearly visible to the user since it affects the whole page (app page meaned here).我已经尝试在 function 中动态更改 styles ,它在点击后呈现 PDF ,但当然更改对用户来说是清晰可见的,因为它影响了整个页面。

Therefore, my question is the following: How could I tell the function html2pdf() which CSS rules using without affecting the page itself?因此,我的问题如下:如何告诉 function html2pdf() CSS 规则使用而不影响页面本身?

You can paste style to head or even a styles file.您可以将样式粘贴到头部,甚至是 styles 文件。 It doesn't work here, but check out codepen.io它在这里不起作用,但请查看codepen.io

If you do not need to support old browsers, you can use the css variables and, for example, after clicking, change the parameters by document.documentElement.style.setProperty("-color", "#000");如果不需要支持旧浏览器,可以使用css变量,例如点击后通过document.documentElement.style.setProperty("-color", "#000");

I know this does not solve the problem, but a change to the html2pdf itself seems very hard to obtain.我知道这并不能解决问题,但是对 html2pdf 本身的更改似乎很难获得。

 const button = document.querySelector(".makePDF"); button.addEventListener("click", (e) => { e.preventDefault(); const style = document.createElement("style"); style.textContent = `body { background: #fff; color: #000; }`; document.head.appendChild(style); const element = document.querySelector("body"); const opt = { margin: 1, filename: "myfile.pdf", image: { type: "jpeg", quality: 0.98 }, html2canvas: { scale: 1 }, jsPDF: { unit: "mm", orientation: "landscape", }, }; html2pdf().set(opt).from(element).save(); });
 body { font-family: nhg-text-bold, arial, sans-serif; font-weight: bold; letter-spacing: -0.4px; background: #000; color: #fff; }.border { height: 400px; width: 890px; border: 1px solid red; }.verLogo { padding-left: 400px; font-size: 25px; }.mainContent h1 { margin-left: 280px; letter-spacing: 1.5px; }.sepLine { border-bottom: 1px solid black; width: 300px; margin-left: 300px; }.UserName, h2, h3 { margin-left: 350px; }
 <script src="https://cdnjs.cloudflare.com/ajax/libs/html2pdf.js/0.10.1/html2pdf.bundle.min.js" integrity="sha512-GsLlZN/3F2ErC5ifS5QtgpiJtWd43JWSuIgh7mbzZ8zBps+dvLusV+eNQATqgA/HdeKFVgA5v3S/cIrLF7QnIg==" crossorigin="anonymous" referrerpolicy="no-referrer"></script> <button type="button" class="makePDF">PDF</button> <div id="content"> <div class="border"> <div class="mainContent"> <h1>CONGRATULATIONS</h1> <div class="sepLine"></div> <span class="UserName"> <h2>xxxxxxxxxxxxx</h2> <h3>You have completed</h3> <h1>Lorem, ipsum dolor.</h1> </span> </div> </div> </div>

Place the appropriate color overrides in a @media print block, and always include that CSS on the page.将适当的颜色覆盖放在@media print块中,并始终在页面上包含 CSS。 For example,例如,

<style>
@media print {
  body {
    background-color: white !important;
    color: black !important;
  }
}
</style>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM