2016-02-23 4 views
0

J'ai besoin d'insérer une image en PDF, mais je reçois une erreur lors de l'insertion, me dit que ce n'est pas formaté correctement. Mon code est le suivant.pdfmake image error insert

public getBase64Image(img : any) { 
     // Create an empty canvas element 
     var canvas = document.createElement("canvas"); 
     canvas.width = img.width; 
     canvas.height = img.height; 

     // Copy the image contents to the canvas 
     var ctx = canvas.getContext("2d"); 
     ctx.drawImage(img, 0, 0); 

     // Get the data-URL formatted image 
     // Firefox supports PNG and JPEG. You could check img.src to 
     // guess the original format, but be aware the using "image/jpg" 
     // will re-encode the image. 
     var dataURL = canvas.toDataURL("image/png"); 

     return dataURL.replace(/^data:image\/(png|jpg);base64,/, ""); 
    } 

la fonction qui convertit base64. que j'appelle la fonction.

exporterPdfHeader:{ 
       columns: [ 
        { 
         width: 300, alignment: 'left', 
         text: [ 
           'Pedido de prueba\n\n', 
           'Centro de Trabajo: prueba. Serie: 00.\n\n', 
           'Numped: 38289', 
           ], 
         bold:true, 
         fontSize: 10, 
         margin: [40, 20, 0, 0], 
         height:120, 
        }, 
        { 
         width: 140, 
         alignment: 'center', 
         image: 'data:image/png;base64, this.getBase64Image("../imagenes/logoExportarPdf.png")', 
         bold:true, 
         fontSize: 10, 
         margin: [40, 20, 0, 0], 
         height:80, 
        } 
       ] 
      }, 

L'erreur

enter image description here

Sur ce, j'ai plusieurs questions. Avez-vous besoin de le passer à base64? Tout ce que je souhaite est d'insérer une photo dans un fichier PDF, ce qui est très simple. Comme un détail ne sais pas si ce sera important, j'utilise Angle ui -grid. Je programme en tapuscrit.

Merci

Répondre

0

vous passez littéralement la chaîne 'données: image/png; base64, this.getBase64Image ("../ Imagenes/logoExportarPdf.png")' comme l'image, ne pas appeler la fonction.

Vous devez utiliser 'data:image/png;base64,'+ this.getBase64Image("../imagenes/logoExportarPdf.png")

+0

merci Ramon! , résolu ma question ^^ – sourcer