这对我有用
使用 jQuery 和https://developer.mozilla.org/en-US/docs/Web/API/Window/open
var $linkToOpenPrintDialog = $('#tvcPrintThisLinkId');
var windowObjectReference = null;
var windowFeatures = 'left=0,top=0,width=800,height=900,menubar=no,toolbar=no,location=yes,resizable=no,scrollbars=no,status=no';
var windowFeaturesStyles = '<link rel="stylesheet" media="print" href="/wp-content/themes/salient-child/dist/css/app-print.css">';
$linkToOpenPrintDialog.on('click', function(event) {
openPrintDialog(this.href, this.target, 'tvcInnerCalculatorDivId', event);
return false;
});
function openPrintDialog(url, windowName, elementToOpen, event) {
var elementContent = document.getElementById(elementToOpen);
if(windowObjectReference == null || windowObjectReference.closed) {
windowObjectReference = window.open( url, windowName, windowFeatures);
windowObjectReference.document.write(windowFeaturesStyles);
windowObjectReference.document.write(elementContent.innerHTML);
windowObjectReference.document.close();
windowObjectReference.focus();
windowObjectReference.print();
windowObjectReference.close();
} else {
windowObjectReference.focus();
};
event.preventDefault();
}
应用程序-print.css
@media print {
body {
margin: 0;
color: black;
background-color: white;
}
}