Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a underline setFontType in jsPDF?

I know that there is 4 types of "setFontType" in jsPDF:

doc.setFontType("normal");
doc.setFontType("italic");
doc.setFontType("bold");
doc.setFontType("bolditalic");

i tried some things but i can't find it, so is there a underline setFontType in jsPDF?

like image 669
Carlos Salles Avatar asked Oct 19 '16 19:10

Carlos Salles


2 Answers

I know this is late but i have a solution for this , you can do this in 2 ways :-

First you can use the inbuilt "line" option to draw a line below your text :-

doc.line(x-from,y-from,x-to,y-to);

Second you can display your text in html and then use the option to print html in your jspdf config for eg:-

<div id="disptext">
    <div class="underText">Hello</div>
</div>
<br/>
<input type='button' value='download' id='downP' />

<style>
.underText { text-decoration: underline;}
</style>

<script>
$(document).ready(function() {
    $('#downP').on('click', function() {
        var pdf = new jsPDF('p', 'pt', 'a4');

        pdf.addHTML($('#disptext')[0], function() {
            pdf.save('text.pdf');
        });
    });
});
</script> 
like image 199
A Biswas Avatar answered Nov 14 '22 22:11

A Biswas


In my case I'm using jsPDF inside of Nativescript, so I couldn't use 'addHTML.'

Adding a little to A Biswas first option I was able to get it to work for dynamic text size like this:

const textWidth = doc.getTextWidth(text);
doc.line(x, y, x + textWidth, y)
like image 44
Felipe Centeno Avatar answered Nov 14 '22 22:11

Felipe Centeno