Home >Web Front-end >CSS Tutorial >How to Capture a Code Block as PDF while Preserving CSS Styling in a Browser?
Downloading Highlighted Pre Element as PDF with CSS Preservation
To save a pre element as PDF while preserving the CSS styling, you can utilize a method that involves opening a new window, injecting the HTML and CSS, and printing to a PDF file.
Solution:
Here's the jQuery code you can use:
$("#save").click(function() { var text = $("#output")[0].outerHTML; var styles = $("style")[0].outerHTML; var popup = window.open("", "popup"); popup.document.body.innerHTML = text + styles; popup.focus(); popup.print(); });
The above is the detailed content of How to Capture a Code Block as PDF while Preserving CSS Styling in a Browser?. For more information, please follow other related articles on the PHP Chinese website!