[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 481
  • Last Modified:

error "Files does not begin with '%PDF-'. when create a file from string base64.

Guys, i got error "Files does not begin with '%PDF-'. when create a file from string base64.

I have a asp source with a pdf coded in string base64, i used a function to decode to binary and when i try to show/create the file i have the error, i think is a charset problem, can you help me please?.


responsePdf.txt
mostrarpdf.txt
0
Marcelitro
Asked:
Marcelitro
  • 5
  • 5
  • 3
2 Solutions
 
DanRollinsCommented:
I can verify that the first eight bytes of the decoded base64 text in the source code file respnsePdf.txt is, indeed:
   %PDF-1.4

I used the base64 decoder here:
    http://makcoder.sourceforge.net/demo/base64.php

I cannot vouch for how well the decoder in your source code works.  I suggest that you test it fully.
0
 
MarcelitroAuthor Commented:
Yes, the base64 string and decode function are OK, my problem is when i try to create the pdf.

I have some type of problem in the following lines:



   gifdata = ConvertB64(strpdf)   
   
   Response.Buffer = True
   Response.Clear
   Response.ContentType = "application/pdf"
   Response.AddHeader "Content-Type", "application/pdf"
   Response.AddHeader "Content-Length", lenB(gifdata)
   
   Response.BinaryWrite gifdata
   Response.Flush
   Response.End()
   
   dim linea
   
   linea = "<a href=|mostrarpdf.asp| target=|_self|>PDF</a>"
   linea = replace(linea, "|", chr(34))
   Response.Write linea 

Open in new window

0
 
MarcelitroAuthor Commented:
I try to save to disk the pdf file, and i can see is saved like that:

HEX:
25 00 50 00 44 00 46 00 2D 00 31 00 2E 00 34 00

DISPLAY
%  .  P  .  D  .  F  .  -  .  1  .  .  .  4  .  

0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
Richard QuadlingSenior Software DeverloperCommented:
Using notepad to remove the non b64-encoded data, I saved the attached a.b64.txt file (yes - I hate notpad for always adding .txt to anything I save, but I was being lazy).

The using this php command line ...

\php5\php -r "file_put_contents('a.pdf', base64_decode(file_get_contents('a.b64.txt')));"

I ended up with a.pdf.

Opening a.pdf opens fine!

Both files attached.
a.b64.txt
a.pdf
0
 
Richard QuadlingSenior Software DeverloperCommented:
Can you supply an online link?

The extra nulls suggest that somewhere the encoding is UTF-16 rather than UTF-8.

Is this only going wrong on a 64bit machine? i.e. on your machine all OK but on the live server "computer says no"?
0
 
MarcelitroAuthor Commented:
Sorry, i cant supply a online link, but is easy copy both files to iis and test responsePDF.asp (responsePDF.txt).

Please your help.
0
 
Richard QuadlingSenior Software DeverloperCommented:
Can you save the PDF in ASP so you have a PDF saved by the server and one saved by your browser. Are they the same? This is to test if the issue is the browser or the server.

Can you try using Firefox with Firebug to monitor the headers being sent. Are there any additional headers being sent by the server which you didn't know about?
0
 
DanRollinsCommented:
It looks to me like there is an implicit conversion going on.  In your statement:

             nGroup = Hex(nGroup)
             nGroup = String(6 - Len(nGroup), "0") & nGroup
             pOut = Chr(CByte("&H" & Mid(nGroup, 1, 2))) & _
                    Chr(CByte("&H" & Mid(nGroup, 3, 2))) & _
                    Chr(CByte("&H" & Mid(nGroup, 5, 2)))
             sOut = sOut & Left(pOut, numDataBytes)
 
the sOut variable is being created as a string of 16-bit values, rather than the 8-bit characters you need.  I'm no guru in VBScript, but I think you need to use the
   ChrB
function in there somewhere.  Perhaps ...

             pOut = ChrB(CByte("&H" & Mid(nGroup, 1, 2))) & _
                    ChrB(CByte("&H" & Mid(nGroup, 3, 2))) & _
                    ChrB(CByte("&H" & Mid(nGroup, 5, 2)))


   Chr Function
   http://msdn.microsoft.com/en-us/library/ws6aa3sf(VS.85).aspx
   (see also the AscB() function)

If that does not work, I suggest (for debugging purposes) breaking that long sequence into assignments of three variables and then concatenating them.  Breakpoint in your code to verify that the sOut is being increased in length by only one byte per variable (rather than the two bytes that is currently happening).

-- Dan
0
 
Richard QuadlingSenior Software DeverloperCommented:
Nice one Dan. As to why CByte() or Chr are returning anything other than 1 byte.

Maybe Chr() is returning a null terminated string and the nulls are being concatenated also.

Odd.

0
 
DanRollinsCommented:
My thought was the implicit conversion of the & operator -- it concatenates strings and in a system where "string" means "sequence of 16-bit UNICODE characters" then each one-byte character gets converted to a 2-byte character as the temporary string is constructed for the concatenation.

If that's the case, then even ChrB() might not work.

In this micrsoft blog,
   http://blogs.msdn.com/ericlippert/archive/2005/04/20/410127.aspx
one contributor points out that VBscript simpoly does not support the concept of a string compsed of 9-bit bytes.  They offer a number of work-arounds, but so far I haven't seen a simple solution.  I'll keep looking...
0
 
DanRollinsCommented:
There is a chance that avoiding the conversion and working only with a byte array might work:

  Dim abData as Byte()
...
    abData(n)= ChrB(...)
    n= n+1

...
Response.BinaryWrite abData
0
 
DanRollinsCommented:
I think I answered it with http:#a21903460
RQuadling's post http:#a21895043  deserves a split for verifying the data and working the problem.
0
 
Richard QuadlingSenior Software DeverloperCommented:
Thanks Dan.
0

Featured Post

Upgrade your Question Security!

Add Premium security features to your question to ensure its privacy or anonymity. Learn more about your ability to control Question Security today.

  • 5
  • 5
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now