Print Only SVG from Browser

后端 未结 2 1779
感情败类
感情败类 2021-01-18 03:47

I\'m working on a webpage that will dynamically render SVG graphics based on user interaction. Once complete, I would like the user to be able to print the graphics only - n

相关标签:
2条回答
  • 2021-01-18 04:15

    You can use jQuery. Assume you have your svg in a DIV(svgDiv) in the web page, include a print button that calls the following, where the root svg has an id=mySVG, to get width/height, or use the svgDiv width/height. This will print the view that is currently in the svg window.

    //---print button---
        var printSVG = function()
        {
            var popUpAndPrint = function()
            {
                var container = $('#svgDiv');
                var width = parseFloat(mySVG.getAttribute("width"))
                var height = parseFloat(mySVG.getAttribute("height"))
                var printWindow = window.open('', 'PrintMap',
                'width=' + width + ',height=' + height);
                printWindow.document.writeln($(container).html());
                printWindow.document.close();
                printWindow.print();
                printWindow.close();
            };
            setTimeout(popUpAndPrint, 500);
        };
    
    0 讨论(0)
  • 2021-01-18 04:19

    You can call window.print to start the printing process from javascript.

    You can make the printed and visible versions different using media queries E.g.

    @media print { different css for print SVG }
    

    If you don't want existing stuff on the page to print, use the media query to set it display:none or visibility:hidden.

    0 讨论(0)
提交回复
热议问题