PureBasic
PureBasic
Insert PDF as Base64 into XML, then Extract back to PDF File
See more XML Examples
Demonstrates how to insert any file into XML using base64 encoding, and then extract back to the original file. This example embeds a PDF in the XML, but the type of file does not matter. It can be any type of file.Chilkat PureBasic Downloads
IncludeFile "CkBinData.pb"
IncludeFile "CkXml.pb"
Procedure ChilkatExample()
success.i = 0
; Load our PDF file.
bd.i = CkBinData::ckCreate()
If bd.i = 0
Debug "Failed to create object."
ProcedureReturn
EndIf
success = CkBinData::ckLoadFile(bd,"qa_data/helloWorld.pdf")
If success <> 1
Debug "Failed to load PDF file."
CkBinData::ckDispose(bd)
ProcedureReturn
EndIf
; Load the following XML:
;
; <?xml version="1.0" encoding="utf-8" ?>
; <something>
; <xyz>
; <abc123>A base64 encoded PDF file will be inserted under this node.</abc123>
; </xyz>
; </something>
xml.i = CkXml::ckCreate()
If xml.i = 0
Debug "Failed to create object."
ProcedureReturn
EndIf
success = CkXml::ckLoadXmlFile(xml,"qa_data/xml/xmlToContainPdf.xml")
If success <> 1
Debug "Failed to load XML file."
CkBinData::ckDispose(bd)
CkXml::ckDispose(xml)
ProcedureReturn
EndIf
; Insert the PDF into the XML.
CkXml::ckNewChild2(xml,"xyz|pdfData",CkBinData::ckGetEncoded(bd,"base64"))
; Show the new XML:
Debug CkXml::ckGetXml(xml)
; The XML now looks like this:
; <?xml version="1.0" encoding="utf-8" ?>
; <something>
; <xyz>
; <abc123>A base64 encoded PDF file will be inserted under this node.</abc123>
; <pdfData>JVBERi0xL ... UlRU9GCg==</pdfData>
; </xyz>
; </something>
; To extract the PDF data out and restore the PDF file:
bd2.i = CkBinData::ckCreate()
If bd2.i = 0
Debug "Failed to create object."
ProcedureReturn
EndIf
success = CkBinData::ckAppendEncoded(bd2,CkXml::ckGetChildContent(xml,"xyz|pdfData"),"base64")
success = CkBinData::ckWriteFile(bd2,"qa_output/helloWorld.pdf")
Debug "Success."
CkBinData::ckDispose(bd)
CkXml::ckDispose(xml)
CkBinData::ckDispose(bd2)
ProcedureReturn
EndProcedure