admin管理员组

文章数量:1279089

I am working on codeigniter. I want to add save to pdf functionality to one of the div. I am using html2canvas to print it. My problem is I want to add a logo & some other information at the header of the pdf, which is not visible on the webpage. I dont know how to do this. Below is my code.

    <script type='text/javascript' src="<?php echo site_url('assets/js/html2canvas.js'); ?>"></script>
    <script type='text/javascript' src="<?php echo site_url('assets/js/jspdf.debug.js'); ?>"></script>

    <script type='text/javascript'>//<![CDATA[ 
    function demoFromHTML() {
         var pdf = new jsPDF('p', 'pt', 'letter');
         pdf.addHTML($('#content')[0], function () {
         pdf.save('test.pdf');
         });

        specialElementHandlers = {
            '#bypassme': function (element, renderer) {
                return true
            }
        };
        margins = {
            top: 30,
            bottom: 30,
            left: 30,
            width: '100%'
            //color: '#000'
        };

        pdf.addHTML()(
        source, // HTML string or DOM elem ref.
        margins.left, // x coord
        margins.top, { // y coord
        'width': margins.width, // max width of content on PDF
        'elementHandlers': specialElementHandlers
        },

        function (dispose) {
            pdf.save('test.pdf');
        }, margins);
    }
    </script>

<div id="content">
Save this to PDF
</div>

Pls help.

I am working on codeigniter. I want to add save to pdf functionality to one of the div. I am using html2canvas to print it. My problem is I want to add a logo & some other information at the header of the pdf, which is not visible on the webpage. I dont know how to do this. Below is my code.

    <script type='text/javascript' src="<?php echo site_url('assets/js/html2canvas.js'); ?>"></script>
    <script type='text/javascript' src="<?php echo site_url('assets/js/jspdf.debug.js'); ?>"></script>

    <script type='text/javascript'>//<![CDATA[ 
    function demoFromHTML() {
         var pdf = new jsPDF('p', 'pt', 'letter');
         pdf.addHTML($('#content')[0], function () {
         pdf.save('test.pdf');
         });

        specialElementHandlers = {
            '#bypassme': function (element, renderer) {
                return true
            }
        };
        margins = {
            top: 30,
            bottom: 30,
            left: 30,
            width: '100%'
            //color: '#000'
        };

        pdf.addHTML()(
        source, // HTML string or DOM elem ref.
        margins.left, // x coord
        margins.top, { // y coord
        'width': margins.width, // max width of content on PDF
        'elementHandlers': specialElementHandlers
        },

        function (dispose) {
            pdf.save('test.pdf');
        }, margins);
    }
    </script>

<div id="content">
Save this to PDF
</div>

Pls help.

Share Improve this question asked Feb 11, 2015 at 12:11 user4423224user4423224 2
  • Have you tried passing your header to the view as a string from your controller. Then adding to the pdf using the text method? – Craig Commented Feb 11, 2015 at 13:05
  • actually i have some hidden content in the page.. which i want to add to the generated pdf file.. – user4423224 Commented Feb 11, 2015 at 13:13
Add a ment  | 

1 Answer 1

Reset to default 8

Before calling to addHTML, you can add images or text to your jsPDF object:

var pdf = new jsPDF('p','pt','a4');

var imgData = 'data:image/jpeg;base64,/9j/4AAQSkZJRgABAQEAXwBfAAD/2wBDAAoHBwkHBgoJCAkLCwoMDxkQDw4ODx4WFxIZJCAmJSMgIyIoLTkwKCo2KyIjMkQyNjs9QEBAJjBGS0U+Sjk/QD3/2wBDAQsLCw8NDx0QEB09KSMpPT09PT09PT09PT09PT09PT09PT09PT09PT09PT09PT09PT09PT09PT09PT09PT09PT3/wgARCAAaABQDAREAAhEBAxEB/8QAGQAAAgMBAAAAAAAAAAAAAAAABQYAAwQB/8QAGAEBAQEBAQAAAAAAAAAAAAAAAwEAAgT/2gAMAwEAAhADEAAAAXKbOK1c92KOHzuQcxaHNjdidpy5yl//xAAfEAACAQMFAQAAAAAAAAAAAAABAgADEhMEEBEhIjH/2gAIAQEAAQUC+QuVq6duEqnoephWKDia/FLjLjt//8QAHREAAgIBBQAAAAAAAAAAAAAAAAIBEQMSEyAiMf/aAAgBAwEBPwEhIZLj2DOttcCkNp7G8xZfH//EAB4RAAIDAAEFAAAAAAAAAAAAAAABAgMREiAhIjFR/9oACAECAQE/AR2ONmS9MolkcZZ8aHDl4b2FTEaEun//xAAhEAABAwMEAwAAAAAAAAAAAAABAAIRAxAxEjJBQiFhYv/aAAgBAQAGPwJQ7acIg8FQWFzfS0B0t+shcpkNqHx1KqahU29rZKybf//EAB0QAQADAQACAwAAAAAAAAAAAAEAESExQVFhgZH/2gAIAQEAAT8hUFrUE1U6+ZZvXITcrvpNdp4xEO+l1b7Gv7BQdYMALdXDkpwD7ipT+kOT/9oADAMBAAIAAwAAABBnmCSOz//EABsRAQACAwEBAAAAAAAAAAAAAAEAESExYSBx/9oACAEDAQE/EAXUQdz5KIsIMuNjTLWFPNMVwaOQoRsVXn//xAAcEQEAAgIDAQAAAAAAAAAAAAABABEhMSBhcVH/2gAIAQIBAT8QUMsIdQ9/JZNpSUTIImK3bZ5AbtfZa3cpbvj/AP/EABwQAQACAwEBAQAAAAAAAAAAAAEAESExQXFRwf/aAAgBAQABPxCsIatahd4Y+dDAb93fjD4HtO4qLlXU0ej2pdETsO11xEdV8cP2hExkSA2d3NHkA0Q0CIxSEyKmjyf/2Q==';
pdf.addImage(imgData, 'JPEG', 20, 20, 20, 26);

pdf.text(50, 40, "Header");

pdf.addHTML(document.body,40,100,function() {
    pdf.save('web.pdf');
});
<script src="https://cdnjs.cloudflare./ajax/libs/jspdf/1.0.272/jspdf.debug.js"></script>
<script src="https://cdnjs.cloudflare./ajax/libs/html2canvas/0.4.1/html2canvas.js"></script>

<body>
    <p id="to-pdf">HTML content...</p>
</body>

Images must be encoded with base64. You can use this tool for that: http://dataurl/#dataurlmaker

本文标签: javascriptHow to add header in pdf generated from HTMLStack Overflow