barak
barak

Reputation: 179

Converting StreamReader back to pdf

I am trying to convert pdf stream back to pdf file, then save it on my server. The function GetHTTPRequest gets pdf url and returns the pdf url stream string. I need to convert this stream to pdf file. My Code:

  public ActionResult Html(string strUrl) 
    {
        string xhr;
        xhr = GetHTTPRequest(strUrl, "GET");
        // make pdf file fron xhr
        return View("Index");

    }

    [ChildActionOnly]
    public static string GetHTTPRequest(string RequestUrl, string RequestMethod)
    {
        try
        {
            HttpWebRequest r = (HttpWebRequest)WebRequest.Create(RequestUrl);
            r.Method = RequestMethod;
            HttpWebResponse res = (HttpWebResponse)r.GetResponse();
            Stream sr = res.GetResponseStream();
            StreamReader sre = new StreamReader(sr);

            string s = sre.ReadToEnd();

            return s;
        }
        catch (Exception)
        {

            return string.Empty;
        }
    }

This is only 1 line from what xhr contains:

"%PDF-1.3\n%�쏢\n5 0 obj\n<>\nstream\nx��\k���V �DB��rq��\r}��_�4U[��\nTҮU�~�ِBI h����>gl����n�N���Y{.�~�33�;�h����뷶.��W7�n����ͭ;[������[կ��?TR��T�_l�6� �O�e�ll��\m\b�����T����F�����V��n���6h嫍��Hk��l�R��O6ô3�7O5�:�.�i<�:���3��S�j��V�O��\b�~&\r�S93�l�ѭ�Q�z�v�cF)���\r��g��m�v����Z���\a���;{�i��Bq�7��^xK�7�U��P?��z>����]���F\b1�X�ec��/4�ji��/К��&#�EjF)�g�D�\v)��_ �\r|��XSEcu�\nILz��H|�ZL�a�wO\af^m6�NzS_lb$�Vx\rB�VG��_���44�v���������w������4J����Q�z���7�刭��+�a�|�7�Z����&��ٕQ��LsE��c�t뜁������)�ad���ӷ2��inSU��-��\a��\fā4ʹ�v1�w�ֽ����)�����S0�����2M�~�S�;�Ԩ2�\a|�����'K�0[\"{��F��5��2�ٱJ[��ӑ ��F�����3��X�s'\b9�[�&�Et��A\"�����N���������)7=�p�T�v.�!�q\"��H����M-i�,��TA��P����tV{]V=z����\rV����T��o�c�s�������[w*i�g��R�EZ�Λ.JQ}�\r:�w)�Ȕڣ�����{�)e%�(GF3�H8�G�Ԣ5)G^u�C�F�ÂQ\v�A=I\"�G\"�@�i�X�3d�����P;%=r���8@\he<�IAv��\a������D[ؕ]���7X��!��\b@B�Y$a5\b \b��T�.���Կ,\n�)1�V�\0N�z�$Pȡ����%

Can someone please put some light on it and solve it for me? I never used stream libary before.

Thanks.

Upvotes: 1

Views: 3904

Answers (1)

pijemcolu
pijemcolu

Reputation: 2605

PDF is a binary file format. Don't use a string it doesn't make sense. What you are doing is eqivalent to trying to open a pdf file with NotePad. You should save the pdf file as file.

This is how you would download the file.

string resourceUrl = "http://www.google.com/file.pdf";
string fileName = downloadedFile.pdf;
WebClient myWebClient = new WebClient();

myWebClient.DownloadFile(resourceUrl,fileName); 

If you need to read file.pdf as a string that's a different matter. You will need to look for a pdf parser. iTextSharp is not a bad choice for a start.

Upvotes: 3

Related Questions