6 votes

How to remove encoded characters from a PDF generated file using jsPDF?

Using the library jsPDF and following this sample code taken from this answer, I am working on a development using HTML and javascript to generate a PDF file.

The content of the PDF file is the HTML code of this test page1 you can download to check your content.

When you open the file in the browser and select the download link; the PDF file is downloaded in the folder "Downloads"to 2, but the result contains some encoded characters:

This is a fragment of the HTML file:

<div class="TituloPagina">
     CONTRATO INDIVIDUAL DE TRABAJO A TÉRMINO FIJO INFERIOR A UN AÑO CELEBRADO ENTRE
     COMPAÑÍA (A). Y FUTURO EMPLEADO (B) –SALARIO ORDINARIO.
</div>

The character is shown encoded in the PDF as well: þÿ

I have tried:

  • Place the property UTF-8 in the HTML file.
  • Save the HTML file as indicated in the response, but in this case, I do not believe that the problem is in the HTML file shown, but in the result of the conversion to PDF.
  • Change the label meta : <meta charset="iso-8859-1" />; which causes the HTML file, you lose the encoding of the characters such as ñ accents, etc

But there is no difference in the generated PDF files.

How to remove encoded characters from a file generated in PDF using jsPDF?


1 I have created this test file, since the real development contains confidential information.

2 The path of the discharge varies depending on the configuration of each user in their browser preference.

3voto

angus Points 426

Everything indicates that it is a bug in the plugin "fromHTML". One solution may be to avoid using characters with code greater than 255 (restricted to characters in Windows-1252).

The other is to use your own jsPDF, by changing this line, where it says:

pdfEscape(text)

by

pdfEscape(text, { 'noBOM': true, 'autoencoding': true })

A third solution: to map the characters to generate the vector of 8-bit.

Maintaining the change in the call to pdfEscape, in the main module, after this line, put

if (ch == 0x2013) ch = 150;
else if (ch == 0x2014) ch = 151;

Do the same with the characters of this table when you differ your Unicode value with its value in the charset (they are the rows "8_" and "9_").

(Disclaimer: it seems right but I have not tested it.)

HolaDevs.com

HolaDevs is an online community of programmers and software lovers.
You can check other people responses or create a new question if you don't find a solution

Powered by:

X