|
|
@@ -68,15 +68,31 @@ export class ReadingPdfComponent implements OnInit {
|
|
|
await new Promise<void>((resolve) => requestAnimationFrame(() => resolve()));
|
|
|
await document.fonts?.ready;
|
|
|
const filename = `${article.title.replace(/[^a-zA-Z0-9一-龥]/g, '_')}_quiz.pdf`;
|
|
|
- await html2pdf().set({
|
|
|
+ let hasBlankTrailingPage = false;
|
|
|
+ const worker = html2pdf().set({
|
|
|
margin: [10, 10, 10, 10], filename,
|
|
|
image: { type: 'jpeg', quality: 1 },
|
|
|
html2canvas: { scale: 1.5, logging: false, useCORS: true, backgroundColor: '#ffffff' },
|
|
|
jsPDF: { unit: 'mm', format: 'a4', orientation: 'portrait' },
|
|
|
- pagebreak: { mode: ['css', 'legacy'], before: '.answer-section', avoid: ['.question-item', '.answer-item'] },
|
|
|
- }).from(element).save();
|
|
|
- } catch {
|
|
|
- this.platform.toast('PDF 导出失败,请重试', 'error');
|
|
|
- } finally { this.exporting.set(false); }
|
|
|
+ pagebreak: {
|
|
|
+ mode: ['css', 'legacy'],
|
|
|
+ before: ['.question-section', '.answer-section'],
|
|
|
+ avoid: ['.question-item', '.answer-item'],
|
|
|
+ },
|
|
|
+ }).from(element).toCanvas().get('canvas', (canvas) => {
|
|
|
+ const pageHeight = Math.floor(canvas.width * (277 / 190));
|
|
|
+ const trailingSlice = canvas.height % pageHeight;
|
|
|
+ hasBlankTrailingPage = trailingSlice > 0 && trailingSlice < 32;
|
|
|
+ }).toPdf();
|
|
|
+ await worker.get('pdf', (pdf) => {
|
|
|
+ const pageCount = pdf.internal.getNumberOfPages();
|
|
|
+ if (hasBlankTrailingPage && pageCount > 1) pdf.deletePage(pageCount);
|
|
|
+ }).save();
|
|
|
+ } catch (error) {
|
|
|
+ const detail = error instanceof Error ? error.message : '';
|
|
|
+ this.platform.toast(detail ? `PDF 导出失败:${detail}` : 'PDF 导出失败,请重试', 'error');
|
|
|
+ } finally {
|
|
|
+ this.exporting.set(false);
|
|
|
+ }
|
|
|
}
|
|
|
}
|