admin 管理员组文章数量: 1087134
I have a fetch request that returns an HTML doc
<!DOCTYPE html><html lang="en"><head>..../body></html>
The next thing I'm trying to do is pass it to an API that will let me download it as a PDF
fetch("data:application/pdf;base64,"+encoded_data...)
I saw that I can use window.btoa() to encode a string into base64. However I'm stuck trying to turn the HTML file into a string, to pass into btoa()
fetch(";})
.then(resp => {return resp.text()})
.then(data => {
//this is the HTML file
var x = String(data)
console.log(x)
var base64 = window.btoa(x)
// console.log(base64)
})
I'm getting this error:
Uncaught (in promise) DOMException: Failed to execute 'btoa' on 'Window': The string to be encoded contains characters outside of the Latin1 range.
at <anonymous>:21:25
I'm assuming this is because what I'm passing btoa isn't a string?
I have a fetch request that returns an HTML doc
<!DOCTYPE html><html lang="en"><head>..../body></html>
The next thing I'm trying to do is pass it to an API that will let me download it as a PDF
fetch("data:application/pdf;base64,"+encoded_data...)
I saw that I can use window.btoa() to encode a string into base64. However I'm stuck trying to turn the HTML file into a string, to pass into btoa()
fetch("https://www.website./cap/people/streamPdf/1214192793"})
.then(resp => {return resp.text()})
.then(data => {
//this is the HTML file
var x = String(data)
console.log(x)
var base64 = window.btoa(x)
// console.log(base64)
})
I'm getting this error:
Uncaught (in promise) DOMException: Failed to execute 'btoa' on 'Window': The string to be encoded contains characters outside of the Latin1 range.
at <anonymous>:21:25
I'm assuming this is because what I'm passing btoa isn't a string?
Share Improve this question edited Sep 13, 2020 at 17:32 dropWizard asked Sep 13, 2020 at 17:24 dropWizarddropWizard 3,55812 gold badges69 silver badges92 bronze badges 2- How exactly are you stuck trying to turn the HTML file into a string? – shreyasm-dev Commented Sep 13, 2020 at 17:26
- updated my question to be more clear – dropWizard Commented Sep 13, 2020 at 17:32
2 Answers
Reset to default 4Replace the html content with a string and try encoding it with base64.
btoa() //base64 encoding
atob() //base64 decoding
Does this answer your question?
fetch("https://example.")
.then(response => {
return response.text()
})
.then(html => {
return window.btoa(html)
})
.then(base64 => {
return "data:text/html;base64," + base64
})
.then(result => {
console.log(result)
})
本文标签: javascriptconverting HTML page into base64Stack Overflow
版权声明:本文标题:javascript - converting HTML page into base64 - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://roclinux.cn/p/1744050396a2524910.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论