Solved

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

Posted on 2008-06-26
16
432 Views
Last Modified: 2013-11-19
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
Comment
Question by:Marcelitro
  • 5
  • 5
  • 3
16 Comments
 
LVL 49

Expert Comment

by:DanRollins
ID: 21892719
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
 

Author Comment

by:Marcelitro
ID: 21893958
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
 

Author Comment

by:Marcelitro
ID: 21893985
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
 
LVL 40

Accepted Solution

by:
RQuadling earned 250 total points
ID: 21895043
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
 
LVL 40

Expert Comment

by:RQuadling
ID: 21895057
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
 

Author Comment

by:Marcelitro
ID: 21896155
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
Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 
LVL 40

Expert Comment

by:RQuadling
ID: 21902903
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
 
LVL 49

Assisted Solution

by:DanRollins
DanRollins earned 250 total points
ID: 21903460
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
 
LVL 40

Expert Comment

by:RQuadling
ID: 21926484
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
 
LVL 49

Expert Comment

by:DanRollins
ID: 21929042
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
 
LVL 49

Expert Comment

by:DanRollins
ID: 21929219
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
 
LVL 49

Expert Comment

by:DanRollins
ID: 25763982
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
 
LVL 40

Expert Comment

by:RQuadling
ID: 25769793
Thanks Dan.
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Read about why website design really matters in today's demanding market.
I've been asked to discuss some of the UX activities that I'm using with my team. Here I will share some details about how we approach UX projects.
Viewers will get an overview of the benefits and risks of using Bitcoin to accept payments. What Bitcoin is: Legality: Risks: Benefits: Which businesses are best suited?: Other things you should know: How to get started:
The viewer will learn how to count occurrences of each item in an array.

743 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now