vba - 在 VBA 中从 Base64 字节数组生成 PDF
问题描述
我有来自 webservice 方法的以下 JSON 响应,它将以 PDF 格式返回 byte[]:
JVBERi0xLjQKJeLjz9MKMSAwI......nVq2P63zkirBH1z9aW3pDxn6F7q9TFV==
返回值是一个 byte() 数组。几天来,我一直在尝试使用“从 VBA 中的 Base64 字符串生成 PDF ”中发布的问题之一中提供的功能来从上述 Base64 字节数组生成 PDF,但我做不到。
我所做的是基于 webservice 方法的返回字节数组,我将其保存为字节数组变量名称b64testByte。随后,我继续在我的程序中调用 2 个方法,即首先是 encodeBase64,然后是 DecodeBase64。生成 PDF 文件。但是,当我尝试打开文件时,出现错误,显示“错误:文档已损坏且无法修复。Adobe Reader 无法打开,因为它不是受支持的文件类型或文件已损坏(例如,它是作为电子邮件附件发送的,但未正确解码)。”
Dim b64testByte() As Byte
b64testByte = xmlhttp.responseText
Dim B64String As String
B64String = encodeBase64(b64testByte)
Dim FilePath As String
FilePath = "D:\label.pdf"
Open FilePath For Binary Access Write As #1
Put #1, 1, DecodeBase64(B64String)
Close #1
Private Function DecodeBase64(ByVal strData As String) As Byte()
Dim objXML As Object 'MSXML2.DOMDocument
Dim objNode As Object 'MSXML2.IXMLDOMElement
'get dom document
Set objXML = CreateObject("MSXML2.DOMDocument")
'create node with type of base 64 and decode
Set objNode = objXML.createElement("b64")
objNode.DataType = "bin.base64"
objNode.Text = strData
DecodeBase64 = objNode.nodeTypedValue
'clean up
Set objNode = Nothing
Set objXML = Nothing
End Function
Private Function encodeBase64(ByRef arrData() As Byte) As String
Dim objXML As Object 'MSXML2.DOMDocument
Dim objNode As Object 'MSXML2.IXMLDOMElement
'get dom document
Set objXML = CreateObject("MSXML2.DOMDocument")
Set objNode = objXML.createElement("b64")
objNode.DataType = "bin.base64"
objNode.nodeTypedValue = arrData
encodeBase64 = objNode.Text
Set objNode = Nothing
Set objXML = Nothing
End Function**
解决方案
推荐阅读
- ios - 我的视图边界开始处的空间
- tcp - 如何根据 TCP 流有效地拆分 pcap 文件?
- google-cloud-platform - 选择了错误的项目进行部署
- javascript - How to set default className in react router link changed
- javascript - jQuery if 语句检查值是否存在
- java - 可执行 jar 在其他系统上不起作用
- visual-studio - Visual Studio - 生成前和生成后执行位置
- vertica - 我们在哪里可以获得 vertica DB 的资源?
- python - 如何在 Pandas 列中从反向修剪字符串
- dojo - 使用 dojo 列中的标题