I have a pdf file embedded on a web page. I want to put a print button the web page so that the end user can print the pdf file. How can I achieve this. below is the code for embedding the pdf file on web browser
If you look into your embedded object it has data property that containing your pdf file source location which we can see as following:
We ought to extract data value from HTML object element and finally need to pass that source location within new content window that will particularly invoked print prompt window.
Let's take a look in action:
Set a button:
<br />
<input type="button"
onclick="printEmbeddedObject()"
class="btn btn-success"
value="Print PDF" />
Note: I have set a onclick function will be invoked while button click event fired.
Script
<script>
let printEmbeddedObject = () => {
const getObjectElement = document.querySelector('object');
let pdfFileLocation = getObjectElement.getAttribute('data');
console.log(pdfFileLocation);
let instanceIframeObject = document.createElement('iframe');
instanceIframeObject.style.visibility = 'hidden';
instanceIframeObject.src = pdfFileLocation;
document.body.appendChild(instanceIframeObject);
instanceIframeObject.contentWindow.focus();
instanceIframeObject.contentWindow.print();
}
</script>
Note: Here, I am extracting embedded object data value and creating a virtual DOM to prompt print dialog. Finally passing the source location which got extracted from onclick event.
Output: