err=File does not begin with %PDF-' with DrawToWeb but not DrawToFile

Skip Navigation LinksHome  /  Support  /  Forums  /  DynamicPDF Generator for COM/ActiveX (v4)  /  err=File does not begin with %PDF-' with DrawToWeb but not DrawToFile

DynamicPDF Generator for COM/ActiveX (v4) Forum

I'm getting an error "File does not begin with '%PDF=' when I DrawToWeb, but I also immediately DrawToFile and the written pdf is just fine and comes up with Acrobat.
Even when I strip it down to just:
Set MyDocument = Server.CreateObject( "DynamicPDF.Document" )
Set MyPage = MyDocument.AddPage()
Set MyLabel = MyPage.AddLabel("123",0,100,100,12)
MyDocument.DrawToWeb
MyDocument.DrawToFile(path & FileName)

the web page gets the error, but then the file is created and displays correctly.

Any ideas? must be something simple that I'm missing because I already generate 100's of pdf's in other pages without any problem.
Posted by a ceTe Software moderator
Hello,

We did some testing on our end using version 4 DynamicPDF Generator for COM/ActiveX product. We are able render the generated PDF document to browser without any problem.

Please output the PDF document in the form of byte array and render the PDF stream in browser using BinaryWrite method instead of using DrawToWeb method and this should work for you. Below is the sample code for it.

<!-- METADATA TYPE="typelib" UUID="{DF9225FE-94A4-490D-8CAD-E8366CE621D3}"-->
<%
    Dim MyDocument
    Set MyDocument = Server.CreateObject( "DynamicPDF.Document" )
    Dim MyPage
    Dim MyLabel
    Set MyPage = MyDocument.AddPage()
    Set Mylabel = MyPage.AddLabel( "Hello World!", 0, 0,100, 12 )
    MyLabel.Font = DPDF_Font_Helvetica
    MyLabel.Fontsize = 12
    Dim MyDocByte          
    MyDocByte = MyDocument.Draw()   
    Response.Clear()
    Response.ContentType = "application/pdf"   
    Response.Buffer = True    
    Response.BinaryWrite( MyDocByte )
    'MyDocument.DrawToWeb
    Set MyPage = Nothing
    Set MyDocument = Nothing
%>

Thanks,
ceTe Software Support Team.
That circumvention works, thanks.  Any ideas why DrawToWeb fails?
Posted by a ceTe Software moderator
Hello,

It is not efficient to call the DrawToWeb and DrawToFile methods on the same Document object as it generates the PDF twice. A better approach would be to call the Draw method to generate the document once and take the PDF byte array and save it to file and send it to browser using the BinaryWrite method. See the sample code below.

<!-- METADATA TYPE="typelib" UUID="{DF9225FE-94A4-490D-8CAD-E8366CE621D3}"-->
<%
        'The METADATA tag above was added to import the constants (i.e. DPDF_Font_Helvetica).
        Option Explicit
        Dim MyDocument, MyPage
        Dim MyStrText
        
        ' Create Document object and set properties
        Set MyDocument = Server.CreateObject( "DynamicPDF.Document" )
        MyDocument.Creator = "HelloWorld.asp"
        MyDocument.Author = "test"
        MyDocument.Title = "Hello World"
        ' Add a page to the document
        Set MyPage = MyDocument.AddPage()
        ' Add a textarea to the page
        MyStrText = "Hello ASP World..." & vbCrLf & "From DynamicPDF™ Generator for COM/ActiveX" & vbCrLf & "DynamicPDF.com"
        MyPage.AddTextArea MyStrText, 0, 0, 504, 100, DPDF_TextAlign_Center, DPDF_Font_Helvetica, 18
        
        ' use the Draw method of output the PDF as byte array.
        Dim MyDocByte        
               MyDocByte = MyDocument.Draw()   
        
        'create a stream object to save the PDF byte array to disk
            Dim BinaryStream
            Set BinaryStream = CreateObject("ADODB.Stream") 
            'Specify stream type - save binary data.
            BinaryStream.Type = 1 
            'Open the stream And write the PDF byte array
            BinaryStream.Open
            BinaryStream.Write MyDocByte 
        'Call to save the PDF to disk
            BinaryStream.SaveToFile Server.MapPath("test123.pdf"), 2
 
        ' take the PDF byte array and stream it to the browser.
        Response.Clear()
            Response.ContentType = "application/pdf"   
        Response.Buffer = True    
            Response.BinaryWrite( MyDocByte )
  
            Set Mypage = Nothing
        Set MyDocument = Nothing          
        Set BinaryStream = Nothing
%>

We were unable to reproduce the error you were getting with DrawToWeb on our end but it could be due to calling the DrawToFile right after or it could be due to the product build you are using which might have an issue with that method, in which case please download and use the latest build from the customer area.

Thanks,
ceTe Software Support Team.

All times are US Eastern Standard time. The time now is 1:48 AM.