|
|
@@ -482,8 +482,7 @@ export async function GeneratePDF(ctx) {
|
|
482
|
482
|
.fill('black');
|
|
483
|
483
|
|
|
484
|
484
|
// 5. 在top:250,left:1443像素,宽537像素,高900像素的空间内竖排显示1-10个单词,每行一个,右对齐
|
|
485
|
|
- doc.font('Helvetica') // 使用常规体(Regular)
|
|
486
|
|
- .fontSize(pixelToPt(48));
|
|
|
485
|
+ doc.font('Helvetica'); // 使用常规体(Regular)
|
|
487
|
486
|
|
|
488
|
487
|
// 计算单词显示区域
|
|
489
|
488
|
const wordAreaTop = pixelToPt(250);
|
|
|
@@ -495,14 +494,31 @@ export async function GeneratePDF(ctx) {
|
|
495
|
494
|
const wordsArray = Array.isArray(words) ? words :
|
|
496
|
495
|
(typeof words === 'string' ? words.split(',') : []);
|
|
497
|
496
|
|
|
498
|
|
- //console.log("Words array for display:", wordsArray);
|
|
499
|
|
-
|
|
500
|
497
|
// 显示单词(最多10个)
|
|
501
|
498
|
const maxWords = Math.min(wordsArray.length, 10);
|
|
502
|
499
|
|
|
503
|
500
|
// 根据单词数量动态计算每个单词的高度空间
|
|
504
|
501
|
const wordHeight = pixelToPt(80); // 固定高度,确保足够的空间显示
|
|
505
|
502
|
|
|
|
503
|
+ // 首先确定所有单词中需要的最小字体大小
|
|
|
504
|
+ let minFontSize = pixelToPt(48); // 默认字体大小
|
|
|
505
|
+ for (let i = 0; i < maxWords; i++) {
|
|
|
506
|
+ const word = wordsArray[i];
|
|
|
507
|
+ if (word) {
|
|
|
508
|
+ const wordLength = word.toString().length;
|
|
|
509
|
+ // 根据词组长度确定需要的字体大小
|
|
|
510
|
+ if (wordLength > 15) {
|
|
|
511
|
+ minFontSize = Math.min(minFontSize, pixelToPt(36)); // 长词组使用较小字体
|
|
|
512
|
+ } else if (wordLength > 10) {
|
|
|
513
|
+ minFontSize = Math.min(minFontSize, pixelToPt(42)); // 中等长度词组使用中等字体
|
|
|
514
|
+ }
|
|
|
515
|
+ }
|
|
|
516
|
+ }
|
|
|
517
|
+
|
|
|
518
|
+ // 使用确定的字体大小显示所有单词
|
|
|
519
|
+ doc.fontSize(minFontSize);
|
|
|
520
|
+ console.log(`Using font size ${minFontSize} for all words`);
|
|
|
521
|
+
|
|
506
|
522
|
for (let i = 0; i < maxWords; i++) {
|
|
507
|
523
|
const word = wordsArray[i];
|
|
508
|
524
|
if (word) { // 确保单词存在
|
|
|
@@ -513,26 +529,11 @@ export async function GeneratePDF(ctx) {
|
|
513
|
529
|
// 添加调试信息
|
|
514
|
530
|
console.log(`Drawing word: "${word}" at position: ${yPosition}`);
|
|
515
|
531
|
|
|
516
|
|
- // 检查词组长度,如果过长则调整字体大小
|
|
517
|
|
- const wordLength = word.toString().length;
|
|
518
|
|
- let fontSize = pixelToPt(48); // 默认字体大小
|
|
519
|
|
-
|
|
520
|
|
- // 根据词组长度动态调整字体大小
|
|
521
|
|
- if (wordLength > 15) {
|
|
522
|
|
- fontSize = pixelToPt(36); // 长词组使用较小字体
|
|
523
|
|
- } else if (wordLength > 10) {
|
|
524
|
|
- fontSize = pixelToPt(42); // 中等长度词组使用中等字体
|
|
525
|
|
- }
|
|
526
|
|
-
|
|
527
|
|
- doc.fontSize(fontSize)
|
|
528
|
|
- .text(word.toString(), wordAreaLeft, yPosition, {
|
|
529
|
|
- width: wordAreaWidth,
|
|
530
|
|
- align: 'right', // 右对齐
|
|
531
|
|
- lineBreak: true // 允许自动换行,处理特别长的词组
|
|
532
|
|
- });
|
|
533
|
|
-
|
|
534
|
|
- // 重置字体大小为默认值,以便下一个词组使用
|
|
535
|
|
- doc.fontSize(pixelToPt(48));
|
|
|
532
|
+ doc.text(word.toString(), wordAreaLeft, yPosition, {
|
|
|
533
|
+ width: wordAreaWidth,
|
|
|
534
|
+ align: 'right', // 右对齐
|
|
|
535
|
+ lineBreak: true // 允许自动换行,处理特别长的词组
|
|
|
536
|
+ });
|
|
536
|
537
|
}
|
|
537
|
538
|
}
|
|
538
|
539
|
}
|