ページの印刷にwindow.print()を使用していますが、ヘッダーとフッターにページタイトル、ファイルパス、ページ番号、日付が含まれています。それらを削除するには?
プリントスタイルシートも試しました。
#header, #nav, .noprint
{
display: none;
}
助けてください。ありがとう。
ページの印刷にwindow.print()を使用していますが、ヘッダーとフッターにページタイトル、ファイルパス、ページ番号、日付が含まれています。それらを削除するには?
プリントスタイルシートも試しました。
#header, #nav, .noprint
{
display: none;
}
助けてください。ありがとう。
回答:
Chromeでは、この自動ヘッダー/フッターを非表示にすることができます
@page { margin: 0; }
内容はページの制限まで拡張されるため、ページ印刷ヘッダー/フッターはありません。もちろん、この場合、コンテンツがページの端まで達しないようにbody要素にマージン/パディングを設定する必要があります。一般的なプリンターは余白なしの印刷を取得できず、おそらく望んだものではないため、次のようなものを使用する必要があります。
@media print {
@page { margin: 0; }
body { margin: 1.6cm; }
}
以下のようマーティンはコメントで指摘ページが長い要素(大きなテーブルのような)1ページ過去のスクロールことを持っている場合、マージンは無視され、印刷されたバージョンが奇妙になります。
この回答の最初の時点(2013年5月)では、Chromeでしか機能しませんでした。対応できないブラウザのサポートが必要な場合は、その場でPDFを作成して印刷できます(JavaScriptを埋め込んだ自己印刷PDFを作成できます)が、それは非常に面倒です。
body { margin: 1.6cm; }
は適切なマージンを無視していたため(おそらくを使用していたためtext-align:right
)、代わりに<div class="container">
コンテンツ用にを作成し、代わりに使用し.container { margin: 1.6cm; }
ました。
body { margin: 1.6cm; }
は、ドキュメント全体のマージンであるため、最初と最後を除いて、マルチページドキュメントの垂直マージンを尊重しないためです。悲しいことに、私は回避策を考えることができません。
私はあなたのcssファイルにこのコードを追加すると問題が解決すると確信しています
<style type="text/css" media="print">
@page
{
size: auto; /* auto is the initial value */
margin: 0mm; /* this affects the margin in the printer settings */
}
</style>
Firefox:
moznomarginboxes
属性を追加<html>
例:
<html moznomarginboxes mozdisallowselectionprint>
CSS標準では、いくつかの高度なフォーマットが可能です。CSSには@pageディレクティブがあり、ページングされたメディア(紙など)にのみ適用されるいくつかのフォーマットを有効にします。http://www.w3.org/TR/1998/REC-CSS2-19980512/page.htmlを参照してください。
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Print Test</title>
<style type="text/css" media="print">
@page
{
size: auto; /* auto is the current printer page size */
margin: 0mm; /* this affects the margin in the printer settings */
}
body
{
background-color:#FFFFFF;
border: solid 1px black ;
margin: 0px; /* the margin on the content before printing */
}
</style>
</head>
<body>
<div>Top line</div>
<div>Line 2</div>
</body>
</html>
そしてFirefoxのためにそれを使用してください
Firefoxでは、https://bug743252.bugzilla.mozilla.org/attachment.cgi?id = 714383(ページのソースを表示::タグHTML)。
あなたのコードでは、置き換え<html>
で<html moznomarginboxes mozdisallowselectionprint>.
@media print {
.footer,
#non-printable {
display: none !important;
}
#printable {
display: block;
}
}
1. ChromeとIEの場合
<script language="javascript">
function printDiv(divName) {
var printContents = document.getElementById(divName).innerHTML;
var originalContents = document.body.innerHTML;
document.getElementById('header').style.display = 'none';
document.getElementById('footer').style.display = 'none';
document.body.innerHTML = printContents;
window.print();
document.body.innerHTML = originalContents;
}
</script>
<div id="div_print">
<div id="header" style="background-color:White;"></div>
<div id="footer" style="background-color:White;"></div>
</div>
例にmoznomarginboxes属性を追加します。
<html moznomarginboxes mozdisallowselectionprint>
手動で印刷したHTMLページのヘッダーとフッターを削除しようとしました。
このページの解決策は私にとってうまくいきませんでしたが、ここで説明されているように自分のフッターを定義するだけでは、Firefoxは自分のヘッダーとフッターを追加しません。
残念ながらこれはChromeでは機能しません。
CSS:
@media screen {
div.divFooter {
display: none;
}
}
@media print {
div.divFooter {
position: fixed;
bottom: 0;
}
}
これをHTMLに追加します。
<div class="divFooter">
<p>UNCLASSIFIED</p>
</div>
``
ここまで下にスクロールすると、Firefoxの解決策が見つかりました。特定のdivのコンテンツをフッターとヘッダーなしで印刷します。必要に応じてカスタマイズできます。
まず、このアドオンJSPrintSetupをダウンロードしてインストールします 。
次に、次の関数を記述します。
<script>
function PrintElem(elem)
{
var mywindow = window.open('', 'PRINT', 'height=400,width=600');
mywindow.document.write('<html><head><title>' + document.title + '</title>');
mywindow.document.write('</head><body >');
mywindow.document.write(elem);
mywindow.document.write('</body></html>');
mywindow.document.close(); // necessary for IE >= 10
mywindow.focus(); // necessary for IE >= 10*/
//jsPrintSetup.setPrinter('PDFCreator'); //set the printer if you wish
jsPrintSetup.setSilentPrint(1);
//sent empty page header
jsPrintSetup.setOption('headerStrLeft', '');
jsPrintSetup.setOption('headerStrCenter', '');
jsPrintSetup.setOption('headerStrRight', '');
// set empty page footer
jsPrintSetup.setOption('footerStrLeft', '');
jsPrintSetup.setOption('footerStrCenter', '');
jsPrintSetup.setOption('footerStrRight', '');
// print my window silently.
jsPrintSetup.printWindow(mywindow);
jsPrintSetup.setSilentPrint(1); //change to 0 if you want print dialog
mywindow.close();
return true;
}
</script>
3番目に、コードで、印刷コードを記述したい場所で次のようにします(私はJQueryを使用しました。プレーンJavaScriptを使用できます)。
<script>
$("#print").click(function () {
var contents = $("#yourDivToPrint").html();
PrintElem(contents);
})
</script>
明らかに、クリックするにはリンクが必要です。
<a href="#" id="print">Print my Div</a>
印刷するdiv:
<div id="yourDivToPrint">....</div>
コードを書く必要はありません。初めてwindow.print()を呼び出す直前に、ブラウザーの印刷設定を変更します。たとえば、Firefoxの場合:
IEのもう1つの例(以前のバージョンでIE 11を使用しているので、FirefoxまたはInternet ExplorerがすべてのページにURLを印刷しないようにすることができます):