jsPDF无法在生产环境中生成PDF(可能是webpack conf的问题))

jogvjijk  于 2023-08-06  发布在  Webpack
关注(0)|答案(1)|浏览(235)

在JHipster生成的Angular应用程序中,我设法在开发模式下通过jsPDF生成PDF。但是当我构建它并将其推到VPS上时,在文档中添加图像会使webApp崩溃。

717.1183bdcffdb5d922.js:1 ERROR TypeError: Cannot read properties of undefined (reading 'data')
    at i.__addimage__.getImageFileTypeByImageData (641.074d6c566f08a36e.js:1:287740)
    at Object.Zt (641.074d6c566f08a36e.js:1:295400)
    at i.addImage (641.074d6c566f08a36e.js:1:295162)
    at m.addImageEntete (287.1ee6c0684b572bd4.js:1:800)
    at M.getPDF (641.074d6c566f08a36e.js:1:47054)
    at M.generatePDFComplet (641.074d6c566f08a36e.js:1:115186)
    at 641.074d6c566f08a36e.js:1:139516
    at d7 (717.1183bdcffdb5d922.js:1:217724)
    at c (717.1183bdcffdb5d922.js:1:217891)
    at HTMLButtonElement.<anonymous> (717.1183bdcffdb5d922.js:1:330709)

字符串
我可以显示他们在地址栏与他们的网址虽然。
下面是一些相关代码:

import { jsPDF, jsPDFOptions } from 'jspdf';
import autoTable, { RowInput } from 'jspdf-autotable';

export const ENTETE_PATH_ABMS = '/content/images/prints/abms_entete.jpg';

@Injectable({
  providedIn: 'root',
})
export class PrintService {
  constructor() {}

  getPDF() {
    const options: jsPDFOptions = {
      orientation: 'p',
      unit: 'mm',
      format: 'a4',
      putOnlyUsedFonts: true,
      floatPrecision: 16, // or "smart", default is 16
    };
    const doc = new jsPDF(options); 
    doc.addImage(ENTETE_PATH_ABMS, 'jpg', posX, posY, w, h);
 }
}

41ik7eoe

41ik7eoe1#

我发现这个问题是jsPDF经常出现的问题:我在完全加载之前添加了图像,并且由于他的性能低于我的开发计算机,所以在生产中出现了这个问题。
在一个著名的聊天机器人的帮助下,我想出了这个解决方案,其中包括observables和html2canvas。

npm install html2cavas rxjs

字符串
在名为CommonUtils的实用程序类中:

export type ImageWrapper = {
   imagePath : string,
   x : number,
   y : number,
   width : number,
   height : number,
 }

  private static loadImage(url: string): Observable<HTMLImageElement> {
return new Observable((observer) => {
  const img = new Image();
  img.crossOrigin = 'Anonymous'; // Allow loading images from external URLs
  img.onload = () => {
    observer.next(img);
    observer.complete();
  };
  img.src = url;
});


}

private static convertImageToCanvas(img: HTMLImageElement, width: number, height: number): Observable<HTMLCanvasElement> {
return from(html2canvas(img, { width, height }));


}

public static addImagesAndSave(images: Array<ImageWrapper>, doc : jsPDF, docname:string): void {
const imageObservables = images.map(({ imagePath }) => this.loadImage(imagePath));

forkJoin(imageObservables)
  .pipe(
    map((loadedImages) => {

      return loadedImages.map((img, index) => {
        document.body.appendChild(img);
        return this.convertImageToCanvas(img, images[index].width, images[index].height)
                    
      }
      );
    }),
    switchMap((canvasObservables) => forkJoin(canvasObservables))
  )
  .subscribe((canvases) => {
    canvases.forEach((canvas, index) => {
      const imgData = canvas.toDataURL('image/jpeg');
      const { x, y, width, height } = images[index];
      console.log('add image ' + ' ' + x + ' ' +  y + ' ' + width + ' ' + height  + ' ' +  imgData  )
      doc.addImage(imgData, 'PNG', x, y, width, height);
    });
    doc.save(docname);
  });


}
这个服务是这样叫的

var imageList : ImageWrapper[] = []
 // set your path , height, width, x and y accordingly
 imageList.push({
  imagePath : '/assets/images.jpeg',
  height : h,
  width : w,
  x : posX,
  y : posY
 })
 CommonUtils.addImagesAndSave(imageList , doc, 'pdfname.pdf')


我仍然要做一些调整,因为与图像的大小问题,但他们加载并显示在PDF格式

相关问题