Skip to content

api2pdf ​

api2pdf is an external service to mainly convert HTML and URLs to PDF format. Furthermore it contains other handy features like PDF merging, document conversion, screenshot maker, ...

Sequence diagram ​

Sensitive data ​

When authenticated data is shown within the generated PDF, the following approach is used.

As an example, lets say we want to generate a PDF with some user's information. The backend implementation would look something like:

1. Request to generate the PDF

ts
// generate-user-pdf.use-case.ts
const userFile = this.fileRepository.create({
  name: `${userUuid}.pdf`,
  mimeType: MimeType.PDF,
  userUuid: userUuid.uuid,
});

const rsaPrivateKey = this.configService.getOrThrow("RSA_PRIVATE");
const token = this.jwtService.sign(
  { userUuid },
  {
    expiresIn: this.EXPIRES_IN_SECONDS,
    privateKey: rsaPrivateKey,
  }
);

const webUrl = getUserWebUrl(userUuid, token); // https://app.com/users/123?token=xyz
const uploadUrl = await this.s3Service.createTemporaryUploadUrl(userFile);
await this.pdfService.generatePdf(webUrl, uploadUrl);

2. Get user info

ts
// get-user-pdf.guard.ts
const rsaPrivateKey = this.configService.getOrThrow("RSA_PRIVATE");
const tokenContent = this.jwtService.verify(token, { secret: rsaPrivateKey });

const userUuid = request.params["uuid"];
if (verifiedToken.userUuid !== userUuid) {
  throw new UnauthorizedError();
}
ts
// app.module.ts
providers: [
  {
    provide: APP_GUARD,
    useClass: GetUserPdfGuard,
  },
];
ts
// get-user.controller.ts
@GetUserPdfGuard()