Files
dees-document/ts/classes.pdfservice.ts

67 lines
1.6 KiB
TypeScript
Raw Normal View History

2025-03-26 14:51:14 +00:00
import * as plugins from "./plugins.js";
import * as helpers from "./helpers.js";
2023-10-16 18:28:12 +02:00
export interface IPdfServiceConstructorOptions {}
2023-10-16 18:28:12 +02:00
/**
* a pdf service for generating pdfs
*/
export class PdfService {
2024-02-16 19:18:20 +01:00
// STATIC
2025-03-26 14:51:14 +00:00
public static async createAndStart(
optionsArg: IPdfServiceConstructorOptions
) {
2024-02-16 19:18:20 +01:00
const pdfService = new PdfService(optionsArg);
await pdfService.start();
return pdfService;
}
2023-10-16 18:28:12 +02:00
// INSTANCE
options: IPdfServiceConstructorOptions;
public smartpdfInstance: plugins.smartpdf.SmartPdf;
constructor(optionsArg: IPdfServiceConstructorOptions) {
this.options = optionsArg;
}
/**
* starts the PdfService
*/
public async start() {
this.smartpdfInstance = new plugins.smartpdf.SmartPdf();
await this.smartpdfInstance.start();
}
/**
* stops the PdfService
*/
public async stop() {
await this.smartpdfInstance.stop();
}
/**
* creates an letter
*/
public async createPdfFromLetterObject(optionsArg: {
2025-03-26 14:51:14 +00:00
letterData: plugins.tsclass.business.TLetter;
documentSettings: plugins.shared.interfaces.IDocumentSettings;
2025-03-26 14:51:14 +00:00
}): Promise<plugins.smartpdf.IPdf> {
2023-10-16 18:28:12 +02:00
const html = `
<script type="module">
${await helpers.getBundleAsString()}
</script>
2025-03-26 14:51:14 +00:00
<dedocument-dedocument printMode documentSettings="${plugins.smartjson.stringifyBase64(
optionsArg.documentSettings
)}" letterData="${plugins.smartjson.stringifyBase64(
optionsArg.letterData
)}"></dedocument-dedocument>
2023-10-16 18:28:12 +02:00
`;
// console.log(html);
2025-03-26 14:51:14 +00:00
const pdfResult = await this.smartpdfInstance.getA4PdfResultForHtmlString(
html
);
2023-10-16 18:28:12 +02:00
return pdfResult;
}
}