Solved

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

Posted on 2008-06-26
16
445 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

 
LVL 40

Accepted Solution

by:
Richard Quadling 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:Richard Quadling
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
 
LVL 40

Expert Comment

by:Richard Quadling
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:Richard Quadling
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:Richard Quadling
ID: 25769793
Thanks Dan.
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
how can i make amazon approved mobile url 4 40
Html fieldset fix its height and width 4 44
Jquery Bold 3 specific words in a paragraph 5 28
calculate days away 11 31
An enjoyable and seamless user experience can go a long way on an eCommerce site. While a cohesive layout and engaging copy play roles in creating a positive user experience, some sites neglect aspects that seem marginal but in actuality prove very …
Developer portfolios can be a bit of an enigma—how do you present yourself to employers without burying them in lines of code?  A modern portfolio is more than just work samples, it’s also a statement of how you work.
The viewer will get a basic understanding of what section 508 compliance can entail, learn about skip navigation links, alt text, transcripts, and font size controls.
Learn how to create flexible layouts using relative units in CSS.  New relative units added in CSS3 include vw(viewports width), vh(viewports height), vmin(minimum of viewports height and width), and vmax (maximum of viewports height and width).

730 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