Plex 2E

Expand all | Collapse all

Converting base64 string to pdf

  • 1.  Converting base64 string to pdf

    Posted Apr 29, 2009 02:35 AM
    I am trying to consume a web service one of the elements of which is a PDF document encoded as a Base64 string.  I need to decode the string to binary and save the document as a PDF so it can be opened by users.  I have written / copied some c++ source code for doing the decoding - see below:  input / output parameters are varycharacter fields, length is a number retrieved using the getlength api  #include

    {
      int i = 0;
      int j = 0;
      int in_ = 0;
      std::string base64_chars =
                            "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
                            "abcdefghijklmnopqrstuvwxyz"
                            "0123456789+/";
      unsigned char char_array_4[4], char_array_3[3];

      char *dst;
      char *src;
      int len, lenout;
      int dstlen = 0;

          len = &(2:);
          src=&(1:).GetBuffer(len);
          lenout = 6 * len / 8;
          if (src[len-1] == '=') lenout = lenout - 1;
          if (src[len-2] == '=') lenout = lenout - 1;
          dst = &(3:).GetBuffer(lenout);


      // Process through encoded string while still valid Base64 characters and not "=" (i.e. filler at end)
      while (len-- && ( src[in_] != '=') && (isalnum(src[in_]) || (src[in_] == '+') || (src[in_] == '/')))
      {
          char_array_4[i++] = src[in_]; in_++;
          if (i ==4) {
              for (i = 0; i > 4);
              char_array_3[1] = ((char_array_4[1] & 0xf) > 2);
              char_array_3[2] = ((char_array_4[2] & 0x3) > 4);
          char_array_3[1] = ((char_array_4[1] & 0xf) > 2);
          char_array_3[2] = ((char_array_4[2] & 0x3)


  • 2.  Re: Converting base64 string to pdf

    Posted May 05, 2009 07:55 AM
    Anyone understand the question?   We are using Plex 6 and Websydian TransactXML 6.   Trying to convert a string to a PDF file - no-one ever done this?


  • 3.  Re: Converting base64 string to pdf

    Posted May 05, 2009 08:30 AM
    While I have done this, I use Java.  Search for open source C++ utilities.   In Java, there are utilities like this in Apache.org.  I then wrap the utility in a Plex source code object. get the generic Java object from the Plex field, (String from the ObCharFld) instantiate the utility invoke the appropriate method of the utility with the String as an input. deal with the returned object I would expect there are as many C++ open source libraries as Java.


  • 4.  Re: Converting base64 string to pdf

    Posted May 05, 2009 11:41 PM
    Thanks Christopher and Simon,  I have already done what you suggested - the code I posted originally is some c++ I got off the net.   It is wrapped in source code and seems to work ok for plain text.However, decoding a pdf does not seem to work - it comes out differently from the original PDF if you look at the file contents in notepad.  Does anyone know anything to look out for in decoding a pdf base64 string?  


  • 5.  Re: Converting base64 string to pdf

    Posted Mar 08, 2010 03:19 AM
    Hi Hughesy and Chris,  Did you guys at Platform or Chris 'Convert a pdf to base64'  I'm curently looking at creating an XML document using TransactXML to contain a pdf doc.  Presently using java imp but  am busy trying to persude people top move to the winc imp.  Am aware of http://www.websydian.com/websydiandoc/v61/source/FAQ/Websydian/Development/30888.htm  But any one got some source pretty please...  CheersGeorge    


  • 6.  Re: Converting base64 string to pdf

    Posted Apr 15, 2010 07:55 AM
    Sorry George,  I just noticed your question.   I dont look on here that often.   Do you still need this?   We do have pdf to base64.  


  • 7.  Re: Converting base64 string to pdf

    Posted Apr 16, 2010 12:01 AM
    Hi Mike,  Well had to do it on my own in the end for java. But may need the WinC version.. or others might do.    Param1 = file to be convertedParam2 = Resulting Base64 string  import java.io.*;
    import sun.misc.BASE64Encoder;{
    try
    {
    byte[] buffer = new byte[(int) new File(&(1:).getValue()).length()];
          FileInputStream f = new FileInputStream(&(1:).getValue());
          String pdffiletext;
          f.read(buffer);
          pdffiletext = new String(buffer);BASE64Encoder encoder = new BASE64Encoder();
    String encodedStr = encoder.encodeBuffer(pdffiletext.getBytes());// System.out.println(encodedStr);
    &(2:).fromString(encodedStr);
    }
    catch(Exception e){;}
    }


  • 8.  Re: Converting base64 string to pdf

    Posted Apr 19, 2010 03:10 AM
    ok - here's the winc version  First call this&1 = Document Name (varchar)&2 = Handle (long)FpKalk = new CStdioFile();char* pFileName = &(1:).GetBuffer(64);
    v->st_returned = ObCharFld(" ");
    if (!FpKalk->Open(pFileName, CFile::modeRead | CFile::shareDenyNone | CFile::typeBinary)) {
      v->st_returned = ObCharFld("ERR");
    }
    else
      &(2:) = (long) FpKalk->m_pStream;  static CStdioFile* FpKalk;  Then call this  &1 = Document string (varchar)&2 = Document length (numeric)  char * InpBuf;
    int   noread;
    InpBuf = &(1:).GetBuffer(1024);

    v->st_returned = ObCharFld(" ");
    if ((noread = FpKalk->Read(InpBuf,1023)) == 0)
      v->st_returned = ObCharFld("EOF");

    &(1:).ReleaseBuffer(noread);
    &(2:) = noread;  Then repeatedly call the below followed   by the above while returned status = successful  &1 = Document string (varchar)&2 = Document length (numeric)&3 = Result string   (varchar)      {
      char *dst;
      char *src;
      int len, lenout;

         len = &(2:);
         src=&(1:).GetBuffer(len);
         lenout = ((len + 2) / 3) * 4;
         dst = &(3:).GetBuffer(lenout);

      char base64_table[] = {"ABCDEFGHIJKLMNOPQRSTUVWXYZ""abcdefghijklmnopqrstuvwxyz""0123456789+/"};
      int x, y = 0, i, flag=0, m=0;
      int n = 3;
      char triple[3], quad[4], c;
     
          for(x = 0; x > 2]; // FC = 11111100
                  quad[1] = base64_table[((triple[0] & 0x03) > 4)]; // 03 = 11
                  quad[2] = base64_table[((triple[1] & 0x0F) > 6)]; // 0F = 1111, C0=11110
                  quad[3] = base64_table[triple[2] & 0x3F]; // 3F = 111111
                  if(n


  • 9.  Re: Converting base64 string to pdf

    Posted May 05, 2009 11:13 PM
    Hi Mike,  We have been writing PDF to XML by using VBScript using the ADODB Stream object, works ok but a C++ option might be better. Below is the code used for tranforming PDF to Base64, the othew way round should work much the same. We are using our own pattern to create XML.  &(3:) = ""
    &(4:) = ""

    Set newNode = xmlDoc.createElement(&(1:))
    Set bin64Node = xmlDoc.createElement("bin64temp")

    ' Use DataType so MSXML will validate the data type
    bin64Node.DataType = "bin.base64"
           
    ' Read in the data
    bin64Node.nodeTypedValue = ReadBinaryFile(&(2:))

    ' get the bin64 data as text
    newNode.Text = bin64Node.Text
    Set bin64Node = Nothing


    Function ReadBinaryFile(FileName)
          adTypeBinary = 1
          Dim BinaryStream
          Set BinaryStream = CreateObject("ADODB.Stream")
         
          If Not (binaryStream Is Nothing) Then      
                binaryStream.Type = adTypeBinary
                binaryStream.Open
                binaryStream.LoadFromFile FileName      
                ReadBinaryFile = binaryStream.Read
                binaryStream.Close
                Set binaryStream = Nothing
          else
                ReadBinaryFile = "error"
                &(3:) = "ERR"
                &(4:) = "Could not create ADODB.Stream object, please check your windows system"
          end if

    End Function  Success,  Simon


  • 10.  Re: Converting base64 string to pdf

    Posted May 05, 2009 11:53 PM
    Mike,  We did decode the base64 stream we packed into the XML using the Stream object, to check if the encoding was ok, and the resulting pdf opened just fine in Acrobat.  Simon


  • 11.  Re: Converting base64 string to pdf
    Best Answer

    Posted May 19, 2009 07:08 AM
    We have a solution if anyone is interested - the problem is the retrieval of the binary into a c++ null terminated string - so c++ thinks the string is shorter than it actually is.  The solution is to use c++ byte arrays and to save the file from within the c++ rather than Plex.   Believe me I did not work out how to do this myself - we have a resident genius.   Here is the source code - 3 parameters - the string to decode, the file to save it to and a returned status in case of error  #include
    {
      // Loop counters etc.
      int i = 0;
      int j = 0;
      int in_ = 0;

      std::string base64_chars =
                            "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
                            "abcdefghijklmnopqrstuvwxyz"
                            "0123456789+/";

      unsigned char char_array_4[4], char_array_3[3];   // Arrays for converting 4 Base64 chars into 3 bytes of decoded data
      char *src;   // Base64 string as a character array
      int len;   // Length of Base64 string
      CStdioFile* fp;   // File used to write out decoded data
      CString ret_sts = "     "; // Returned status

      // Access the Base64 string as an array of bytes
      len = &(1:).GetLength();
      src=&(1:).GetBuffer(len);

      // Open output file
      fp = new CStdioFile();
      if (!fp->Open(&(2:), CFile::modeCreate | CFile::modeWrite | CFile::shareDenyWrite | CFile::typeBinary)) {
          ret_sts = "ERR";  
      }                                              

      // Read encoded string char by char while still valid Base64 characters and not "=" (i.e. filler at end).
      // For each block of 4 chars read, decode and write out resulting 3 chars.
      while ((ret_sts == "     ") && len-- && ( src[in_] != '=') && (isalnum(src[in_]) || (src[in_] == '+') || (src[in_] == '/')))
      {
          char_array_4[i++] = src[in_]; in_++;

          if (i ==4) {
              for (i = 0; i > 4);
              char_array_3[1] = ((char_array_4[1] & 0xf) > 2);
              char_array_3[2] = ((char_array_4[2] & 0x3) Write(char_array_3, 3);
              i = 0;
          }
      }
      // Process last 1-3 characters if total length (excluding trailing "=") wasn't a multiple of 4
      if (i) {
          for (j = i; j > 4);
          char_array_3[1] = ((char_array_4[1] & 0xf) > 2);
          char_array_3[2] = ((char_array_4[2] & 0x3) Write(char_array_3, j);
      }
      // Close output file
      fp->Close();

      // Set Returned Status
      &(3:) = ObCharFld(ret_sts);  
    }