I want to create a PDF with
new File(["<body>text<body>"],"application/pdf")
But I want it without any libs
With Vanilla Js
And get base 64 string with new FileReader Also tried URL.createObjectURL
it returns a URL but if I open it, after some time it closes automatically Also, how can I add data to a new file for pdf Like => new File([data] , ...)
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)
Or with blob
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)
CodePudding user response:
To write mime type text/pdf i.e. a pdf without binary lib encodings you simply need to write a string like this (save as text.pdf to see how it works)
%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
or use in an 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>