Support » Plugin: Document Gallery » Adding Print button next to each PDF

Viewing 4 replies - 1 through 4 (of 4 total)
  • Plugin Author Dan Rossiter

    (@danrossiter)

    Hi Vipworks,

    This certainly can be done 🙂 The approach would be similar to what was discussed here: https://wordpress.org/support/topic/creating-document-gallery-button-beside-add-media-button/

    You would add a button to each document’s output that opened the file in a new window and printed that window (see here: https://developer.mozilla.org/en-US/docs/Web/API/Window/print).

    -Dan

    Plugin Author demur

    (@demur)

    Hi Vipworks!

    Like Dan has mentioned that some principles are already there among provided solutions 🙂
    As You know usually there is a pile of different ways to achieve desired result. I happened to have all the parts of the puzzle in Dan’s way. (Another way for instance is to use, they say good, library PDF.js; I’ve seen some people managed to print PDFs without visually opening them, if interested You can google it ;-))

    So, here is code that will add a print link below every PDF thumbnail element of Document Gallery. On click new tab will be opened and print dialog emerge; upon print dialog closing the tab would be closed automatically.

    To use this code You should paste it to the functions.php file of Your active WordPress theme, this can be done either using Dashboard -> Appearance -> Editor -> Theme Functions (for the active theme), or through FTP.

    function dg_pdf_print_prepare() { ?>
    <script type="text/javascript">
    	jQuery(document).ready( function() {
    		jQuery('.document-icon a[href$=".pdf"]').parent().append('<a href="javascript:void(0);" class="dg-pdf-print">🖶 Print</a>');
    		jQuery('.dg-pdf-print').on('click', dgPDFprint);
    		function dgPDFprint() {
    			var printWindow = window.open(jQuery(this).prev('a').attr('href'), '_blank');
    			printWindow.onload = function() {
    				setTimeout(function () {
    					printWindow.print();
    					printWindow.onfocus = function () { setTimeout(function () { printWindow.close(); }, 100); }
    				}, 500);
    			}
    		}
    	});
    </script>
    <?php }
    add_action('wp_print_footer_scripts', 'dg_pdf_print_prepare');

    I hope this would work for You.
    Please don’t hesitate to let us know if You run into any further issues.

    PS: If You’ve found our answers and/or the plugin useful, please rate the efforts. Thanks!

    Vipworks

    (@pooja_gajrani)

    Hello Demur and Dan,

    Thank you so much for the quick support. I tried to add this code snippet and this works great !! But the problem in it only works for the page 1 if there are lots of PDFs and there is pagination. if we click on the page 2, it doesn’t shows up there and then if we click again on the page 1, it doesn’t shows up there too.

    Could you please suggest any solution for this?

    Plugin Author demur

    (@demur)

    Hi Vipworks!

    I assumed that such a trifle You could do by Yourself 😉
    A little tweak and here You are 🙂

    function dg_pdf_print_prepare() { ?>
    <script type="text/javascript">
    	function dgPDFprint() {
    		var printWindow = window.open(jQuery(this).prev('a').attr('href'), '_blank');
    		printWindow.onload = function() {
    			setTimeout( function () {
    				printWindow.print();
    				printWindow.onfocus = function () { setTimeout( function () { printWindow.close(); }, 100); }
    			}, 500);
    		}
    	}
    	function dgPDFbuttons() {
    		var freshDG = jQuery('.document-gallery').not('[data-dg-pdf-print]').attr('data-dg-pdf-print', true);
    		freshDG.find('.document-icon a[href$=".pdf"]').parent().append('<a href="javascript:void(0);" class="dg-pdf-print">🖶 Print</a>');
    		freshDG.find('.dg-pdf-print').on('click', dgPDFprint);
    	}
    	jQuery(document).ready( function() {
    		dgPDFbuttons();
    		jQuery('.dg-paginate-wrapper').parent().on("DOMSubtreeModified", dgPDFbuttons);
    	});
    </script>
    <?php }
    add_action('wp_print_footer_scripts', 'dg_pdf_print_prepare');

    I hope this would finally work for You.
    Please don’t hesitate to let us know if You run into any further issues.

    PS: If You’ve found our answers and/or the plugin useful, please rate the efforts. Thanks!

Viewing 4 replies - 1 through 4 (of 4 total)
  • The topic ‘Adding Print button next to each PDF’ is closed to new replies.