Can anyone tell how to print custom documents with HTML5? This is what i want with this print function. I have document management system that already have print function build using silverlight. I want a solution to replace silverlight plugin from HTML5. please give me any idea or a solution.
Asked
Active
Viewed 3,378 times
-5
-
do you mean print, as in with ink on paper using a physical printer? or are you trying to convert arbitrary documents into HTML? – Woodrow Barlow Jul 16 '15 at 17:57
2 Answers
1
Assuming you are already displaying the document in the webpage using Iframe
,
If the assumption is correct, you can print the content of the iframe
as following code
<input type="button" onclick="printIframe()" value="print" />
<iframe id="printf" name="printf" src="./sample.pdf"></iframe>
<script>
function printIframe() {
var target = document.getElementById('printf');
try {
target.contentWindow.document.execCommand('print', false, null);
} catch (e) {
target.contentWindow.print();
}
}
</script>
Or else if you are not using Iframe
, just write a custom print css file to display only the content of the document you want to print.
Note: You don't have direct access to the printer using javascript

Chamika Sandamal
- 23,565
- 5
- 63
- 86
-
thank you for your idea. this was help full me for documents that have one page. if my pdf have more than one page what can be the solution? – Mewan Jul 19 '15 at 02:53
-
This work for any number of pages. this will open the browser print dialog and you can print all pages, range of pages or given page by changing the `pages` option of the print dialog. – Chamika Sandamal Jul 19 '15 at 05:53
0
You can customize the style of what HTML5 looks like when it is printed with css @media print rules. For instance if you wanted to make the font-size larger when printed you would do,
p{
font-size: 15px
}
@media print {
p{
font-size: 20px;
}
}

Charlie Brumbaugh
- 219
- 1
- 5
- 15