add pdf maker
This commit is contained in:
@@ -0,0 +1,2 @@
|
|||||||
|
registry=https://mirror-npm.runflare.com
|
||||||
|
# registry=https://package-mirror.liara.ir/repository/npm/
|
||||||
@@ -1,4 +1,4 @@
|
|||||||
import { Controller, Get, Post, Body, Patch, Param, Delete, UseGuards } from '@nestjs/common';
|
import { Controller, Get, Post, Body, Patch, Param, Delete, UseGuards, Res } from '@nestjs/common';
|
||||||
import { PrintService } from '../provider/print.service';
|
import { PrintService } from '../provider/print.service';
|
||||||
import { CreateSectionDto } from '../dto/create-section.dto';
|
import { CreateSectionDto } from '../dto/create-section.dto';
|
||||||
import { ApiBearerAuth, ApiOperation } from '@nestjs/swagger';
|
import { ApiBearerAuth, ApiOperation } from '@nestjs/swagger';
|
||||||
@@ -6,6 +6,8 @@ import { AdminAuthGuard } from '../../auth/guards/adminAuth.guard';
|
|||||||
import { UpdateSectionDto } from '../dto/update-section.dto';
|
import { UpdateSectionDto } from '../dto/update-section.dto';
|
||||||
import { Permissions } from 'src/common/decorators/permissions.decorator';
|
import { Permissions } from 'src/common/decorators/permissions.decorator';
|
||||||
import { PermissionEnum } from 'src/common/enums/permission.enum';
|
import { PermissionEnum } from 'src/common/enums/permission.enum';
|
||||||
|
import type { FastifyReply } from 'fastify';
|
||||||
|
|
||||||
|
|
||||||
@Controller('admin')
|
@Controller('admin')
|
||||||
@UseGuards(AdminAuthGuard)
|
@UseGuards(AdminAuthGuard)
|
||||||
@@ -52,4 +54,26 @@ export class PrintController {
|
|||||||
return this.printService.remove(id);
|
return this.printService.remove(id);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// front end must send form with its selected fields
|
||||||
|
@Post('pdf')
|
||||||
|
@Permissions(PermissionEnum.VIEW_PRINT)
|
||||||
|
@ApiOperation({ summary: 'Create Pdf' })
|
||||||
|
async createPDf(@Body() dto: CreateSectionDto, @Res() res: FastifyReply) {
|
||||||
|
const html = `
|
||||||
|
<html>
|
||||||
|
<body>
|
||||||
|
<h1>Hello from NestJS PDF</h1>
|
||||||
|
<p>This PDF was generated using Puppeteer.</p>
|
||||||
|
</body>
|
||||||
|
</html>
|
||||||
|
`;
|
||||||
|
|
||||||
|
const pdfBuffer = await this.printService.generatePdf(html);
|
||||||
|
|
||||||
|
res
|
||||||
|
.header('Content-Type', 'application/pdf')
|
||||||
|
.header('Content-Disposition', 'attachment; filename="file.pdf"')
|
||||||
|
.send(pdfBuffer);
|
||||||
|
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -7,6 +7,7 @@ import { Print } from '../entities/print.entity';
|
|||||||
import { UpdateSectionDto } from '../dto/update-section.dto';
|
import { UpdateSectionDto } from '../dto/update-section.dto';
|
||||||
import { FieldService } from 'src/modules/form-builder/provider/field.service';
|
import { FieldService } from 'src/modules/form-builder/provider/field.service';
|
||||||
import { PrintWithFields } from '../interface/print';
|
import { PrintWithFields } from '../interface/print';
|
||||||
|
// import * as puppeteer from 'puppeteer';
|
||||||
|
|
||||||
|
|
||||||
@Injectable()
|
@Injectable()
|
||||||
@@ -80,4 +81,20 @@ export class PrintService {
|
|||||||
|
|
||||||
return printSection
|
return printSection
|
||||||
}
|
}
|
||||||
|
|
||||||
|
async generatePdf(html: string): Promise<Buffer> {
|
||||||
|
// const browser = await puppeteer.launch();
|
||||||
|
// const page = await browser.newPage();
|
||||||
|
|
||||||
|
// await page.setContent(html, { waitUntil: 'networkidle0' });
|
||||||
|
|
||||||
|
// const pdf = await page.pdf({
|
||||||
|
// format: 'A4',
|
||||||
|
// printBackground: true,
|
||||||
|
// });
|
||||||
|
|
||||||
|
// await browser.close();
|
||||||
|
// return pdf;
|
||||||
|
return true as any
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user