Búsqueda de sitios web

typescript - HTML a PDF usando jsPDF soporte RTL


Estoy intentando convertir HTML a PDF con jsPDF usando Angular 5
Aquí está mi código:

import * as jsPDF from "jspdf";
.
.
.
htmlToPdf(){
var doc=new jsPDF();
var specialElementHandlers = {
  '#content' : function(element,render) {return true;}
};

doc.fromHTML(document.getElementById('content'), 20,20,{
           'width':500,
      'elementHandlers': specialElementHandlers,
});

doc.save('Reports.pdf');
}

Estoy intentando exportar el PDF con dirección de derecha a izquierda sin éxito
Cuando intento este comando:

doc.viewerPreferences({Direction: 'R2L'});

Recibí un error: 'la propiedad no existe en el tipo "jsPdf"'
Por cierto, en mi HTML o CSS traté de poner el atributo direction pero no ayuda. Aún así va de izquierda a derecha

Respuesta #1

En mi componente usé esta función:

htmlToPdf(){
    const elementToPrint = document.getElementById('content');
    const pdf = new jsPDF();
    pdf.addHTML(elementToPrint, () => {
        pdf.save('report.pdf');
    });

En mi HTML: puse una etiqueta div que envuelve todo... y con id="content" uso el atributo dir="rtl"

<div id="content" dir="rtl">
.
.
.
</div>

No olvides agregar este script:

<script src="https://cdnjs.cloudflare.com/ajax/libs/html2canvas/0.4.1/html2canvas.js"></script>

Artículos relacionados