Anirudh Agarwal
Anirudh Agarwal

Reputation: 697

PDF generated is blank

 private static void DownloadFile()
        {
            FtpWebRequest reqFTP;
            WebResponse webResponse;
            GetTheResponseFromFTP(out reqFTP, out webResponse, true);

            FtpWebResponse response = (FtpWebResponse)webResponse;

            Stream responseStream = response.GetResponseStream();
            StreamReader reader = new StreamReader(responseStream);

            using (StreamWriter streamWriter =
                  new StreamWriter("d:\\TestUnity.pdf", true))
            {
                streamWriter.WriteLine(reader.ReadToEnd());
            }


            reader.Close();
            response.Close();  
        }

I have the above function, that download a file from the FTP location. I am reading the text and trying to write it in a file in my local machine. The PDf file generated is of the same size as it is downloaded but when I open the file its blank. Now I have two questions:

  1. Can any one suggest how to save the downloaded file to a path which can be changed.
  2. Whats the reason for the above problem mentioned.

Upvotes: 1

Views: 1980

Answers (2)

Jugdeep
Jugdeep

Reputation: 1

Here is very simple code which works for me

    void GeneratePDF(WebResponse response)
    {
        using (var streamFile = File.Create("E:/JSS.pdf"))
        response.GetResponseStream().CopyTo(streamFile);
    }

Upvotes: 0

Sayse
Sayse

Reputation: 43300

From the documentation.

StreamWriter implements a TextWriter for writing characters to a stream

This means you haven't created a pdf file but a textfile with the *.pdf extension.

There are multiple utilities available to create a pdf

WkHtmlToPDF and ITextSharp are just two

Upvotes: 1

Related Questions