Pdfkit 输出是乱码

Pdfkit output is garbled

在我的节点项目上,我想 运行 np 审计。这需要新版本的 npm。 运行审核后,我需要更新很多 npm 模块,并将节点从 6.92 升级到 12.3.1。

我的 pdf 在此之前工作正常。

这是我现在在我的 pdf 上得到的输出:

%PDF-1.3
%ÿÿÿÿ
7 0 obj
<<
/Type /Page
/Parent 1 0 R
/MediaBox [0 0 612 792]
/Contents 5 0 R
/Resources 6 0 R

endobj
6 0 obj
<<
/ProcSet [/PDF /Text /ImageB /ImageC /ImageI]
/Font <<
/F1 8 0 R

endobj
5 0 obj
<<
/Length 220
/Filter /FlateDecode

stream
xœµQ1N�1ìó
�€`;öx#�¶@‚‚�)�¢8�¶»‚ÿ7x�$ÄmÃ�$ÒX±¬ñÌDˆóÞHBt¥Ó¹|�ÙôîÆwS(˜0µÚ&£q.·�B¢4–ò|ð†† ��Ç ®l� 
ƒ·ãLÎtPVnÚ¸Iëú–¨�”™ø…Æc¹�åé/ûÑ«¯[�ïWÞËã^±±a«‰%8��º—Ò¤�¦kδÜÛ1‘×�œÑÑfÒŒ�¯á‘=è×;Y/ 
±á=g2Ɯ֬�¶W‰F�:6J2ô�%f¡«W,Èó�Äë&㫯ú�=]‚ú
endstream
endobj
10 0 obj
(PDFKit)
endobj
11 0 obj
(PDFKit)
endobj
12 0 obj
(D:20191210115354Z)
endobj
9 0 obj
<<
/Producer 10 0 R
/Creator 11 0 R
/CreationDate 12 0 R

endobj
8 0 obj
<<
/Type /Font
/BaseFont /Helvetica
/Subtype /Type1
/Encoding /WinAnsiEncoding

endobj
4 0 obj
<<

endobj
3 0 obj
<<
/Type /Catalog
/Pages 1 0 R
/Names 2 0 R

endobj
1 0 obj
<<
/Type /Pages
/Count 1
/Kids [7 0 R]

endobj
2 0 obj
<<
/Dests <<
/Names [
]

endobj
xref
0 13
0000000000 65535 f
0000000841 00000 n
0000000898 00000 n
0000000779 00000 n
0000000758 00000 n
0000000208 00000 n
0000000119 00000 n
0000000015 00000 n
0000000661 00000 n
0000000586 00000 n
0000000500 00000 n
0000000525 00000 n
0000000550 00000 n
trailer
<<
/Size 13
/Root 3 0 R
/Info 9 0 R
/ID [ ]

startxref
945
%%EOF

这是我的 package.json 依赖项:

"dependencies": {
"@types/express": "^4.17.2",
"@types/morgan": "^1.7.37",
"@types/request": "^2.48.3",
"@types/serve-favicon": "^2.5.0",
"@types/shelljs": "^0.8.6",
"body-parser": "^1.19.0",
"cookie-parser": "^1.4.4",
"core-js": "^3.4.8",
"crypto-js": "^3.1.9-1",
"debug": "^2.6.9",
"express": "^4.17.1",
"fontkit": "^1.8.0",
"fs": "0.0.1-security",
"fs-extra": "^8.0.1",
"helmet": "^3.21.2",
"http-proxy-middleware": "^0.20.0",
"jade": "^1.11.0",
"linebreak": "^1.0.2",
"morgan": "^1.9.1",
"node": "^13.3.0",
"nodemon": "^1.19.1",
"npm-run-all": "^4.1.5",
"pdfkit": "^0.10.0",
"png-js": "^1.0.0",
"request": "^2.88.0",
"rimraf": "^2.6.3",
"serve-favicon": "^2.5.0",
"shelljs": "^0.8.3",
"ts-node": "^8.2.0",
"typescript": "^3.7.3"

这是我生成 pdf 的方式:

var PDFDocument = require('pdfkit');
var fs=require('fs');
var doc = new PDFDocument();
doc.pipe( fs.createWriteStream('out.pdf') );  

  let xval : number = 70
  let yval : number = 100

  doc.text("Employee:  "+schedule.name, xval, yval)
  yval += 25;

  doc.text("Schedule Date:  "+schedule.scheduledate, xval, yval)

  doc.text("  ")
  for (var ins of schedule.scheduleinstructions)
  {
    yval += 25;
    doc.text(ins.time + "  "+ ins.instruction)
    doc.text("  ")
  }

doc.pipe( res );
doc.end();

关于这个问题有什么想法吗?

想通了。

实际上是头盔导致了问题。

我只需要摆脱这一行:

app.use(头盔())

编辑:

进一步调查这个问题,导致问题的具体头盔 header 是 app.use(helmet.noSniff())