This is a migrated thread and some comments may be shown as answers.

ExportToPdf: File does not begin with '%PDF-'

6 Answers 75 Views
Grid
This is a migrated thread and some comments may be shown as answers.
jlj30
Top achievements
Rank 2
jlj30 asked on 11 Mar 2014, 06:02 PM
Hi,

I am exporting a RadGrid and directing the output to a server file in the OnGridExporting event.
The file is successfully created and if I open it with NotePad the file does in fact begin with %PDF-.
But when I attempt to open it in my browser, I get the error you see in the title of this thread.
I cannot open it directly with Adobe Reader either.

I used the technique identified in this thread to create the file (although it was for an xls file):

Here are the relevant code pieces:

Declaration:
<telerik:RadGrid ID="grdRACI" Skin="Outlook" GridLines="Both" runat="server" AutoGenerateColumns="true" OnGridExporting="grdRACI_Exporting"
                            OnItemDataBound="grdRACI_ItemDataBound" OnColumnCreated="grdRACI_ColumnCreated" >
                            <MasterTableView NoMasterRecordsText="No responsibilities defined" NoDetailRecordsText="No responsibilities defined">
                            </MasterTableView>
                            <ClientSettings>
                                <Scrolling AllowScroll="true" UseStaticHeaders="true" />
                                <Resizing AllowColumnResize="true" AllowResizeToFit="true" ResizeGridOnColumnResize="true" />
                            </ClientSettings>
                            <ExportSettings OpenInNewWindow="false" HideStructureColumns="true">
                            </ExportSettings>
                        </telerik:RadGrid>

Code that performs the export:
...
grdRACI.ExportSettings.Pdf.PageTitle = "RACI Chart For - " + Session["currentProcessName"].ToString();
        grdRACI.ExportSettings.Pdf.DefaultFontFamily = "Arial Narrow";
        grdRACI.ExportSettings.Pdf.PageLeftMargin = Unit.Parse("0.25in");
        grdRACI.ExportSettings.Pdf.PageRightMargin = Unit.Parse("0.25in");
        Session["RACI_PDF_Filename"] = "ExternalDocs/" + "RACI_Chart_ For_" + Session["currentProcessName"].ToString() + " - " + DateTime.Now.ToString("ddMMMyyyy-HHmm") + ".pdf";
        grdRACI.MasterTableView.ExportToPdf();
...

Code that creates the file:
protected void grdRACI_Exporting(object source, GridExportingArgs e)
    {
        string path = Server.MapPath("~/" + Session["RACI_PDF_Filename"].ToString());
        using (FileStream fs = File.Create(path))
        {
            Byte[] info = System.Text.Encoding.Unicode.GetBytes(e.ExportOutput);
            fs.Write(info, 0, info.Length);
        }
        Response.Redirect(Request.Url.ToString());
    }


What am I doing wrong?

Thanks in advance for any suggestions.

Jim

6 Answers, 1 is accepted

Sort by
0
jlj30
Top achievements
Rank 2
answered on 12 Mar 2014, 12:40 PM
It seems that I cannot attach a pdf file to this post, so I have attached a screenshot of a partial hex dump of the file.
Jim
0
jlj30
Top achievements
Rank 2
answered on 12 Mar 2014, 12:41 PM
Let's try the attachment again.
0
Kostadin
Telerik team
answered on 14 Mar 2014, 09:50 AM

Hello Jim,

Please try using the approach from the following sample which demonstrates how to download a file to the server for all export formats.

Regards,

Kostadin
Telerik
 

DevCraft Q1'14 is here! Watch the online conference to see how this release solves your top-5 .NET challenges. Watch on demand now.

 
0
jlj30
Top achievements
Rank 2
answered on 14 Mar 2014, 04:38 PM
Hi,

I looked at the sample you identified.
It looks like I'm doing the same thing with one exception:

In the GridExporting event handler I have the following statement:

            Byte[] info = System.Text.Encoding.Unicode.GetBytes(e.ExportOutput);

Where your sample has:

            byte[] output = Encoding.GetEncoding(WesternEuropean).GetBytes(e.ExportOutput);

This statement will not compile in my VS2013 environment - WesternEuropean does not exist in the current context.

With my code, rhe file is produced and I can see it on the server.  I just can't open it up, either thru my browser or my using Adobe Reader natively on the server.  Is there something wrong with my encoding statement above?

By the way, I can't seem to attach a pdf file to this post, so here's a hex dump of the first 32 bytes of the generated file:

25 00 50 00 44 00 46 00 2D 00 31 00 2E 00 33 00 0D 00 0A 00 25 00 E2 00 E3 00 CF 00 D3 00 0D 00

translated:

%.P.D.F.-.1...3.....%.â.ã.Ï.Ó...

Any further advice?

Thanks

Jim
0
Accepted
Kostadin
Telerik team
answered on 19 Mar 2014, 08:58 AM
Hi Jim,

It looks like the code which you are using to encode the exported output generates a broken file. The WesterEuropean property represent an integer value equal to 1252. You can find it defined as a private integer at the beginning of the Default class. You could use the following method to encode export output.
byte[] output = Encoding.GetEncoding(1252).GetBytes(e.ExportOutput);

Regards,
Kostadin
Telerik
 

DevCraft Q1'14 is here! Watch the online conference to see how this release solves your top-5 .NET challenges. Watch on demand now.

 
0
jlj30
Top achievements
Rank 2
answered on 19 Mar 2014, 12:55 PM
Hi Kostadin,

The solution you provided works perfectly.
Thanks

Jim
Tags
Grid
Asked by
jlj30
Top achievements
Rank 2
Answers by
jlj30
Top achievements
Rank 2
Kostadin
Telerik team
Share this question
or