Generate pdf from HTML in div using Javascript

前端 未结 12 1321
你的背包
你的背包 2020-11-22 06:10

I have the following html code:



    
        

don\'t print th

12条回答
  •  情书的邮戳
    2020-11-22 06:38

    One way is to use window.print() function. Which does not require any library

    Pros

    1.No external library require.

    2.We can print only selected parts of body also.

    3.No css conflicts and js issues.

    4.Core html/js functionality

    ---Simply add below code

    CSS to

    @media print {
            body * {
                visibility: hidden; // part to hide at the time of print
                -webkit-print-color-adjust: exact !important; // not necessary use         
                   if colors not visible
            }
    
            #printBtn {
                visibility: hidden !important; // To hide 
            }
    
            #page-wrapper * {
                visibility: visible; // Print only required part
                text-align: left;
                -webkit-print-color-adjust: exact !important;
            }
        }
    

    JS code - Call bewlow function on btn click

    $scope.printWindow = function () {
      window.print()
    }
    

    Note: Use !important in every css object

    Example -

    .legend  {
      background: #9DD2E2 !important;
    }
    

提交回复
热议问题