Topic Options
#53489 - 05/27/16 09:38 AM Converting a PDF to base64 variable for DB write
DanielD Offline
OL Newbie

Registered: 03/12/14
Posts: 7
Hello I was looking to create a single workflow process to read in a file then generate a PDF then encode that PDF to base64 so i can use the planet press DB action to write that encoded pdf to a sql server db. I see the digital action and create pdf options but nether seem to have the output pdf to base64. Any other plugins or ideas on how to go about this?

Thanks in advance.

Top
#53491 - 05/27/16 11:16 AM Re: Converting a PDF to base64 variable for DB write [Re: DanielD]
Philippe F. Offline
OL Expert

Registered: 09/06/00
Posts: 1931
Loc: Objectif Lune, Montreal, Qc
You can use a script. Since Google is everybody's best friend, I found this little piece of code, which, after sligthly adapting it to the Workflow context, sets JobInfo 9 to a BASE64 version of the current job file (which would be, in your case, the PDF).

You can then do whatever you want with JobInfo 9.

Code:
Option Explicit

'' Code adapted from
'' https://ghads.wordpress.com/2008/10/17/vbscript-readwrite-binary-encodedecode-base64/
'' Many thanks to the author!

' common consts
Const TypeBinary = 1
Const ForReading = 1, ForWriting = 2, ForAppending = 8

Dim inFile
inFile = Watch.GetJobFileName

Dim inByteArray, base64Encoded, base64Decoded, outByteArray
inByteArray = readBytes(inFile)
base64Encoded = encodeBase64(inByteArray)
base64Decoded = decodeBase64(base64Encoded)

Watch.SetJobInfo 9,base64Encoded

private function readBytes(file)
  dim inStream
  ' ADODB stream object used
  set inStream = CreateObject("ADODB.Stream")
  ' open with no arguments makes the stream an empty container
  inStream.Open
  inStream.type= TypeBinary
  inStream.LoadFromFile(file)
  readBytes = inStream.Read()
end function

private function encodeBase64(bytes)
  dim DM, EL
  Set DM = CreateObject("Microsoft.XMLDOM")
  ' Create temporary node with Base64 data type
  Set EL = DM.createElement("tmp")
  EL.DataType = "bin.base64"
  ' Set bytes, get encoded String
  EL.NodeTypedValue = bytes
  encodeBase64 = EL.Text
end function

private function decodeBase64(base64)
  dim DM, EL
  Set DM = CreateObject("Microsoft.XMLDOM")
  ' Create temporary node with Base64 data type
  Set EL = DM.createElement("tmp")
  EL.DataType = "bin.base64"
  ' Set encoded String, get bytes
  EL.Text = base64
  decodeBase64 = EL.NodeTypedValue
end function

private Sub writeBytes(file, bytes)
  Dim binaryStream
  Set binaryStream = CreateObject("ADODB.Stream")
  binaryStream.Type = TypeBinary
  'Open the stream and write binary data
  binaryStream.Open
  binaryStream.Write bytes
  'Save binary data to disk
  binaryStream.SaveToFile file, ForWriting
End Sub
_________________________
Technical Product Manager
I don't want to achieve immortality through my work; I want to achieve immortality through not dying - Woody Allen

Top
#53501 - 05/31/16 09:55 AM Re: Converting a PDF to base64 variable for DB write [Re: DanielD]
DanielD Offline
OL Newbie

Registered: 03/12/14
Posts: 7
Thanks for the help, I was able to get this to work as you have outlined above.

Top
#56120 - 07/16/18 05:58 PM Re: Converting a PDF to base64 variable for DB write [Re: DanielD]
Uomo Del Ghiaccio Online   content
OL Expert

Registered: 02/21/01
Posts: 666
Any success converting a Base64 object back into an image or PDF file?
_________________________
Uomo Del Ghiaccio
--------------------

Top
#56121 - 07/18/18 06:12 AM Re: Converting a PDF to base64 variable for DB write [Re: DanielD]
Philippe F. Offline
OL Expert

Registered: 09/06/00
Posts: 1931
Loc: Objectif Lune, Montreal, Qc
The following should work:
Code:
'' Sample syntax:
'' Base64StringToFile(MyBase64String, "C:\Test\DecodedFile.pdf")

function Base64StringToFile(b64String, filename)
	Dim inByteArray
	inByteArray = b64String
	writeBytes filename, decodeBase64(inByteArray)
end function

function decodeBase64(base64)
	dim DM, EL
	Set DM = CreateObject("Microsoft.XMLDOM")
	' Create temporary node with Base64 data type
	Set EL = DM.createElement("tmp")
	EL.DataType = "bin.base64"
	' Set encoded String, get bytes
	EL.Text = base64
	decodeBase64 = EL.NodeTypedValue
end function

sub writeBytes(file, bytes)
	Dim binaryStream
	Set binaryStream = CreateObject("ADODB.Stream")
	binaryStream.Type = TypeBinary
	'Open the stream and write binary data
	binaryStream.Open
	binaryStream.Write bytes
	'Save binary data to disk
	binaryStream.SaveToFile file, ForWriting
end sub

_________________________
Technical Product Manager
I don't want to achieve immortality through my work; I want to achieve immortality through not dying - Woody Allen

Top