javascript 生成PDF以供下载并在新窗口中打开

nqwrtyyt  于 2022-12-10  发布在  Java
关注(0)|答案(1)|浏览(254)

我想使用new File(["<body>text<body>"],"application/pdf")创建PDF,但不想创建任何库
“香草味"
并使用新FileReader获取base 64字符串还尝试了URL.createObjectURL
它返回一个URL,但如果我打开它,一段时间后,它会自动关闭此外,我如何添加数据到一个新的文件为pdf喜欢=〉新文件([数据],...)

const file = new File(['<body>Text</body>'] , "app.pdf")
//Or. 
const file = new File(['<body>Text</body>'] , "app.pdf" , {type: 'application/pdf'})

const D = new FileReader()
D.onload = ()=> {console.log(this.result || D.result)}
D.readAsDataURL(file)

或带斑点

const file = new File(['<body>Text</body>'] , "app.pdf" )
//Or.
const file = new File(['<body>Text</body>'] , "app.pdf" , {type: "application/pdf"})

URL.createObjectURL(file)
s71maibg

s71maibg1#

要编写mime类型的text/pdf,即没有二进制lib编码的pdf,您只需编写一个字符串,如下所示(保存为text.pdf,查看其工作原理)或右键单击并从https://github.com/GitHubRulesOK/MyNotes/raw/master/MAKE-PDF.cmd下载演示pdf cmd writer

%PDF-1.1
%âãÏÓ
1 0 obj<</Type/Catalog/Pages 2 0 R>>endobj
2 0 obj<</Type/Pages/Kids [3 0 R]/Count 1/MediaBox [0 0 594 792]>>endobj
3 0 obj<</Type/Page/Parent 2 0 R/Resources<</Font<</F1<</Type/Font/Subtype/Type1/BaseFont/Helvetica>>>>>>/Contents 4 0 R>>endobj
4 0 obj<</Length 78
>>
stream

BT /F1 18 Tf 036 740 Td (Body) Tj ET
BT /F1 18 Tf 036 720 Td (Text) Tj ET

endstream 
endobj xref
0 5 
0000000000 65535 f
0000000021 00000 n
0000000065 00000 n
0000000139 00000 n
0000000269 00000 n
trailer<</Root 1 0 R /Size 5>>
startxref
401
%%EOF

或在iFrame中使用

<iframe title="testing" type="application/pdf" width="98%" height="98%" src="data:application/pdf;base64,JVBERi0xLjENCiXDosOjw4/Dkw0KMSAwIG9iajw8L1R5cGUvQ2F0YWxvZy9QYWdlcyAyIDAgUj4+ZW5kb2JqDQoyIDAgb2JqPDwvVHlwZS9QYWdlcy9LaWRzIFszIDAgUl0vQ291bnQgMS9NZWRpYUJveCBbMCAwIDU5NCA3OTJdPj5lbmRvYmoNCjMgMCBvYmo8PC9UeXBlL1BhZ2UvUGFyZW50IDIgMCBSL1Jlc291cmNlczw8L0ZvbnQ8PC9GMTw8L1R5cGUvRm9udC9TdWJ0eXBlL1R5cGUxL0Jhc2VGb250L0hlbHZldGljYT4+Pj4+Pi9Db250ZW50cyA0IDAgUj4+ZW5kb2JqDQo0IDAgb2JqPDwvTGVuZ3RoIDc4DQo+Pg0Kc3RyZWFtDQoNCkJUIC9GMSAxOCBUZiAwMzYgNzQwIFRkIChCb2R5KSBUaiBFVA0KQlQgL0YxIDE4IFRmIDAzNiA3MjAgVGQgKFRleHQpIFRqIEVUDQoNCmVuZHN0cmVhbSANCmVuZG9iaiB4cmVmDQowIDUgDQowMDAwMDAwMDAwIDY1NTM1IGYNCjAwMDAwMDAwMjEgMDAwMDAgbg0KMDAwMDAwMDA2NSAwMDAwMCBuDQowMDAwMDAwMTM5IDAwMDAwIG4NCjAwMDAwMDAyNjkgMDAwMDAgbg0KdHJhaWxlcjw8L1Jvb3QgMSAwIFIgL1NpemUgNT4+c3RhcnR4cmVmDQo0MDEgJSVFT0YNCg==">Your browser does not support iFrame</Iframe>

相关问题