Frage

As jspdf does not support utf8 characters, I'm trying to replace some of the characters with images and then export it in pdf. To help you understand, I made the sample of code, where I try to put an image in html div, and then paste this into the pdf with jspdf. The text appears in pdf, but no image, so whats the issue? I am a beginner, so please explain me how to do this.

TRY THIS LINK AND SEE THE PROBLEM: http://www.balkanex.info/test/start.html

And the code is below:

start.html

<!doctype>
<html>
<head>
    <meta charset="utf-8">
    <script type="text/javascript" src="runner.js" charset="utf-8"></script>
    <script type="text/javascript" src="script.js" charset="utf-8"></script>
    <script type="text/javascript" src="jspdf/jquery/jquery-1.11.0.min.js"></script>
    <script type="text/javascript" src="jspdf/jspdf.js"></script>
    <script type="text/javascript" src="jspdf/libs/Deflate/adler32cs.js"></script>
    <script type="text/javascript" src="jspdf/libs/FileSaver.js/FileSaver.js"></script>
    <script type="text/javascript" src="jspdf/libs/Blob.js/BlobBuilder.js"></script>
    <script type="text/javascript" src="jspdf/jspdf.plugin.addimage.js"></script>
    <script type="text/javascript" src="jspdf/jspdf.plugin.standard_fonts_metrics.js"></script>
    <script type="text/javascript" src="jspdf/jspdf.plugin.addimage.js"></script>
    <script type="text/javascript" src="jspdf/jspdf.plugin.split_text_to_size.js"></script>
    <script type="text/javascript" src="jspdf/jspdf.plugin.from_html.js"></script>
</head>

<body>
    <input type="button" id="btn" value="PROCEED TO THE NEXT PAGE" onClick="run();pdf();">
</body>
</html>

runner.js

function run() {
    document.write('<div id="myid">This is the image: <img src="button.png" alt="Submit"> </div><br/>');
    document.write('<button id="pdf">Export in pdf</button></div>');
}

script.js

function pdf() {
$(function () {
    var doc = new jsPDF();
    var specialElementHandlers = {
        'body': function (element, renderer) { 
            return true;
        }};
    $('#pdf').click(function () {
        doc.fromHTML($('#myid').html(), 15, 35, {
            'width': 170,
                'elementHandlers': specialElementHandlers
        });
        doc.save('sample.pdf');
    });
});
}
War es hilfreich?

Lösung

The library you use jspdf does not support some of the utf-8 letters, so I suggest you to use some other lib (like Mozilla's pdf.js) and avoid these problems.

If you, on the other hand, decide to use this one - then I you will have to make an array for each variable that saves the string and then replace chars with images.

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top