Printen via iframe
Hi,
Ik zoek een mooie manier waarmee ik een html-pagina kan printen via een button op een andere pagina.
Dus: ik heb een pagina met een print-button, en die moet een andere html-pagina printen die ook op de server staat.
Dat klinkt makkelijk, maar ik zoek er een die in meerdere browsers werkt. En die heb ik nog niet gevonden.
Iemand tips?
Ik zoek een mooie manier waarmee ik een html-pagina kan printen via een button op een andere pagina.
Dus: ik heb een pagina met een print-button, en die moet een andere html-pagina printen die ook op de server staat.
Dat klinkt makkelijk, maar ik zoek er een die in meerdere browsers werkt. En die heb ik nog niet gevonden.
Iemand tips?
Welke heb je gevonden? Misschien dat er tegen een beveiliging wordt gehikt?
Werkt dit wel?
https://stackoverflow.com/a/473350
Verder staan er in die thread nog meer handige tips.
https://stackoverflow.com/a/473350
Verder staan er in die thread nog meer handige tips.
Gewijzigd op 12/04/2018 15:38:12 door - Ariën -
Wat ik nog niet helemaal begrijp, heb dit in de iframepagina:
Dit in de pagina met de button:
Maar hoe maak ik nu een button die print.php?ID=XXXX oproept?
Dit in de pagina met de button:
Code (php)
1
2
3
4
5
6
7
8
2
3
4
5
6
7
8
function printIframe(id)
{
var iframe = document.frames ? document.frames[id] : document.getElementById(id);
var ifWin = iframe.contentWindow || iframe;
iframe.focus();
ifWin.printPage();
return false;
}
{
var iframe = document.frames ? document.frames[id] : document.getElementById(id);
var ifWin = iframe.contentWindow || iframe;
iframe.focus();
ifWin.printPage();
return false;
}
Maar hoe maak ik nu een button die print.php?ID=XXXX oproept?
Je geeft het iframe bij de definitie een id mee. Bijvoorbeeld <iframe id=“samenvatting” > en bij de button <button onclick=“printIframe(‘samenvatting’);”>PRINT</button>
En klaar is het. Als je pagina wilt instellen, bijvoorbeeld geen achtergrondfiguur, zoek dan eens op @print css.
En klaar is het. Als je pagina wilt instellen, bijvoorbeeld geen achtergrondfiguur, zoek dan eens op @print css.
Ik heb letterlijk het volgende:
Link:test
print.html
paginametbutton.html
Maar er gebeurt niks als je op de print-knop drukt... in geen een browser... Wat gaat er mis?
Link:test
print.html
Code (php)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
2
3
4
5
6
7
8
9
10
11
12
13
14
15
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>Untitled Document</title>
<script>
function printPage() { print(); }
</script>
</head>
<body>
Deze pagina moet geprint worden.
</body>
</html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>Untitled Document</title>
<script>
function printPage() { print(); }
</script>
</head>
<body>
Deze pagina moet geprint worden.
</body>
</html>
paginametbutton.html
Code (php)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>Untitled Document</title>
<script>
function printIframe(id)
{
var iframe = document.frames ? document.frames[id] : document.getElementById(id);
var ifWin = iframe.contentWindow || iframe;
iframe.focus();
ifWin.printPage();
return false;
}
</script>
</head>
<body>
<iframe url="print.html" id="test"></iframe>
<button onclick="printIframe('test');">DE GROTE PRINTKNOP</button>
</body>
</html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>Untitled Document</title>
<script>
function printIframe(id)
{
var iframe = document.frames ? document.frames[id] : document.getElementById(id);
var ifWin = iframe.contentWindow || iframe;
iframe.focus();
ifWin.printPage();
return false;
}
</script>
</head>
<body>
<iframe url="print.html" id="test"></iframe>
<button onclick="printIframe('test');">DE GROTE PRINTKNOP</button>
</body>
</html>
Maar er gebeurt niks als je op de print-knop drukt... in geen een browser... Wat gaat er mis?
Gewijzigd op 13/04/2018 14:25:23 door Daan Vee
Wat zegt de JS-debugger?
Hier heb je een werkende functie:
Succes!
Code (php)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>Untitled Document</title>
<script>
function printFrame(id) {
var frm = document.getElementById(id).contentWindow;
frm.focus();// focus on contentWindow is needed on some ie versions
frm.print();
return false;
}
</script>
</head>
<body>
<iframe url="print.html" id="test"> </iframe>
<button onclick="printFrame('test');">DE GROTE PRINTKNOP</button>
</body>
</html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>Untitled Document</title>
<script>
function printFrame(id) {
var frm = document.getElementById(id).contentWindow;
frm.focus();// focus on contentWindow is needed on some ie versions
frm.print();
return false;
}
</script>
</head>
<body>
<iframe url="print.html" id="test"> </iframe>
<button onclick="printFrame('test');">DE GROTE PRINTKNOP</button>
</body>
</html>
Succes!
Hi Jan,
Super, de printfuctie wordt nu geladen.
Enige probleem is dat de preview in de printfunctie helemaal leeg is. En dat is print.html niet.
Gaat er nog iets mis bij het laden?
Super, de printfuctie wordt nu geladen.
Enige probleem is dat de preview in de printfunctie helemaal leeg is. En dat is print.html niet.
Gaat er nog iets mis bij het laden?
Ja, het moet iframe src zijn en niet iframe url.
Thanks!
Het werkt nu in Safari en Firefox... Chrome niet, IE weet ik niet.
Valt daar nog wat aan te doen?
Ik heb nu dit:
test.html
print.html
Het werkt nu in Safari en Firefox... Chrome niet, IE weet ik niet.
Valt daar nog wat aan te doen?
Ik heb nu dit:
test.html
Code (php)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>Untitled Document</title>
<script>
function printFrame(id) {
var frm = document.getElementById(id).contentWindow;
frm.focus();// focus on contentWindow is needed on some ie versions
frm.print();
return false;
}
</script>
</head>
<body>
<iframe src="print.html" id="test"> </iframe>
<button onclick="printFrame('test');">DE GROTE PRINTKNOP</button>
</body>
</html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>Untitled Document</title>
<script>
function printFrame(id) {
var frm = document.getElementById(id).contentWindow;
frm.focus();// focus on contentWindow is needed on some ie versions
frm.print();
return false;
}
</script>
</head>
<body>
<iframe src="print.html" id="test"> </iframe>
<button onclick="printFrame('test');">DE GROTE PRINTKNOP</button>
</body>
</html>
print.html
Code (php)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
2
3
4
5
6
7
8
9
10
11
12
13
14
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>Untitled Document</title>
<script>
function printPage() { print(); }
</script>
</head>
<body>
Hier alles wat geprint moet worden...
</body>
</html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>Untitled Document</title>
<script>
function printPage() { print(); }
</script>
</head>
<body>
Hier alles wat geprint moet worden...
</body>
</html>
Probeer het eens met frm.printPage() in plaats van frm.print(). Dan gebruik je de print functie binnen de pagina zodat het door Chrome en Safari niet als onveilig bestempeld wordt.
Dan gebeurt er bij mij nog niks in Chrome, bij jou wel Ben?
Safari en Firefox werkt wel...
IE kan ik niet testen....
Safari en Firefox werkt wel...
IE kan ik niet testen....
Gewijzigd op 14/04/2018 19:49:29 door Daan Vee
Zo werkt het in Firefox, IE, Edge en Chrome. Safari kan ik niet testen:
test.html:
print.html:
test.html:
Code (php)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>Untitled Document</title>
<script>
function printFrame(id) {
var frm = document.getElementById(id).contentWindow;
frm.focus();
frm.postMessage('print', '*');
return false;
}
</script>
</head>
<body>
<iframe src="print.html" id="test"> </iframe>
<button onclick="printFrame('test');">DE GROTE PRINTKNOP</button>
</body>
</html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>Untitled Document</title>
<script>
function printFrame(id) {
var frm = document.getElementById(id).contentWindow;
frm.focus();
frm.postMessage('print', '*');
return false;
}
</script>
</head>
<body>
<iframe src="print.html" id="test"> </iframe>
<button onclick="printFrame('test');">DE GROTE PRINTKNOP</button>
</body>
</html>
print.html:
Code (php)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>Untitled Document</title>
<script>
(function() {
window.addEventListener('message', function(event) {
if (event.data == 'print') {
window.print();
}
});
})();
</script>
</head>
<body>
Hier alles wat geprint moet worden...
</body>
</html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>Untitled Document</title>
<script>
(function() {
window.addEventListener('message', function(event) {
if (event.data == 'print') {
window.print();
}
});
})();
</script>
</head>
<body>
Hier alles wat geprint moet worden...
</body>
</html>
Gewijzigd op 14/04/2018 21:06:16 door Ben van Velzen
Super het werkt, zelfs in Safari!
Nog 1 ding, hoe zorg ik ervoor dat het iframe in alle browsers verborgen is?
Dit lijkt niet te werken...?
Nog 1 ding, hoe zorg ik ervoor dat het iframe in alle browsers verborgen is?
Dit lijkt niet te werken...?
visibility: hidden al geprobeerd?
Edit:
Ik lees ook dat HTML5 een hidden argument heeft voor haast elke tag.
Edit:
Ik lees ook dat HTML5 een hidden argument heeft voor haast elke tag.
Gewijzigd op 14/04/2018 21:46:47 door - Ariën -
display:none werkt wel maar zal niets printen.
visibility: hidden; width:0; height: 0 verbergt voor mij correct.
visibility: hidden; width:0; height: 0 verbergt voor mij correct.
Speel anders eens met opacity-properties, en eventueel zelfs met de @media print
Gewijzigd op 14/04/2018 22:01:08 door - Ariën -
Super, alles werkt! Thanks allemaal!
Welke oplossing heb je nu toegepast?




