Pdfkit:如何为添加的每个新页面生成新的PDF?

时间:2019-06-28 15:53:13

标签: node.js typescript web-services server-side pdfkit

我正在使用pdfkit创建一个新的PDF,并使用node-typescript进行编码,我希望将每个新页面都放在一个单独的PDF文件中(仅包含一页)。 问题是我不知道PDF内容的大小,它来自客户端。 (如果与我想做的事情有关)

我尝试将pdf.end放入'pageAdded'函数中,但每次尝试创建pdf时仍会得到此空白pdf。 (我对节点/打字稿不了解)

这是我的代码:

IMPORTS

  @Component({
    selector: 'app-center-pictures',
    templateUrl: 'center-pictures.component.html',
    providers: [PictureService],
    styleUrls: ['center-pictures.component.css']
  })
export class CenterPicturesComponent implements OnInit {

  @Input() center: Center;
  facilities: CenterFacilities[];
  pictureUrl = environment.pictureUrl + '/';

  @ViewChild('updateData') updateData: TemplateRef<any>;
  window: NbWindowRef;

  pictures: PictureData[];
  uploads: UploadPicture[] = [`enter code here`];
  uploading: boolean = false;

  error: string;
  success: string;
  disabled: boolean = false;

  constructor(
    private pictureService: PictureService,
    private centersService: CentersService,
    private windowService: NbWindowService
  ) { }

  ngOnInit() {
    this.centersService.getCenterFacilities(this.center.id)
      .subscribe(
        facilities => {
          this.facilities = facilities
        },
        error => this.error = error.message
      )
    this.getCenterPictures();
  }

  getCenterPictures() {
    this.pictureService.getCenterPictures(this.center.id)
      .subscribe(
        pictures => this.pictures = pictures,
        error => this.error = error.message
      );
  }

  processFile(image) {
    this.error = null;
    this.success = null;
    if (image.files.length) {
      let upload = new UploadPicture(image.files[0]);
      this.uploads.push(upload);
    }
  }

  removeUpload(upload) {
    const index = this.uploads.indexOf(upload);
    this.uploads.splice(index, 1);
  }

  uploadPictures() {
    this.error = null;
    this.success = null;
    if (!this.uploads.length) {
      return this.error = 'Please choose a picture to upload';
    }
    this.uploads.forEach((upload) => {
      this.uploading = true;
      this.pictureService.addCenterPicture(this.center.id, upload.picture())
        .subscribe(
          picture => {
            this.success = '\'' + picture.name + '\' has been uploaded';
            this.getCenterPictures();
            this.uploading = false;
          },
          error => this.error = error.message
        );
    });
    this.uploads.length = 0;
  }

  openWindow(picture: PictureData) {
    this.window = this.windowService.open(this.updateData, {
      title: picture.name,
      context: { picture: picture },
    });
  }

  updatePicture(picture: PictureData) {
    this.error = null;
    this.success = null;
    this.pictureService.updateCenterPicture(this.center.id, picture.id, {
      caption: picture.caption,
      activity: picture.activity,
      facilityId: picture.facilityId,
      isIndoor: picture.isIndoor
    }).subscribe(
      () => {
        this.success = '\'' + picture.name + '\' has been successfully updated';
        if (this.window)
          this.window.close();
      },
      error => this.error = error.message
    );
  }

  removePicture(id: string, name: string) {
    this.success = null;
    this.error = null;
    if (!this.disabled) {
      this.disabled = true;
      this.pictureService.deleteCenterPicture(this.center.id, id)
        .subscribe(
          () => {
            this.getCenterPictures();
            this.success = '\'' + name + '\' has been successfully removed';
            this.disabled = false;
          },
          error => this.error = error.message
        );
    }
  }
}

您使用哪个操作系统? Arch Linux Manjaro 有人知道我该怎么做吗?

0 个答案:

没有答案