PDF Download from Rest Service in Blank

2,042 views
Skip to first unread message

nelssen....@gmail.com

unread,
Apr 2, 2015, 9:34:37 AM4/2/15
to europass...@googlegroups.com
Hello, 

I'm Trying to integrate your solution with the website that I'm currently developing and it looks that all is going well but the PDF Download is in Blank.

Just to test I do the POST Request with the sample xml available in the Europass website here and the response is OK which is great.

Then after downloading the PDF it is just Blank.

I read that REST API expects all data to be encoded in utf8, but what about the response?

Can you please guide me to find a solution?

Thanks in Advance,

This is the code that I'm using: 

//POST REQUEST    
            string xmlContent = File.ReadAllText("D:\\XML\\CV.xml");
            
            HttpWebRequest request = (HttpWebRequest)WebRequest.Create(URL);
            request.Method = "POST";
            request.ContentType = "application/xml";
            using (Stream webStream = request.GetRequestStream())
            using (StreamWriter requestWriter = new StreamWriter(webStream, System.Text.Encoding.UTF8))
            {
                requestWriter.Write(xmlContent);
            }


            //GET RESPONSE
            try
            {
                WebResponse webResponse = request.GetResponse();
                using (Stream webStream = webResponse.GetResponseStream())
                {
                    if (webStream != null)
                    {
                        using (StreamReader responseReader = new StreamReader(webStream))
                        {
                            string response = responseReader.ReadToEnd();
                            byte[] byteResponse = Encoding.UTF8.GetBytes (response);
                            Response.Clear();
                            Response.Buffer = true;
                            Response.ContentType = "application/pdf";
                            Response.AddHeader("content-disposition", "attachment;filename=Test.pdf");// to open file prompt Box open or Save 
                            Response.AddHeader("Content-Length", byteResponse.Length.ToString());
                            Response.Charset = "";
                            Response.Cache.SetCacheability(HttpCacheability.NoCache);
                            Response.BinaryWrite(byteResponse);
                            Response.End();
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                Console.Out.WriteLine("-----------------");
                Console.Out.WriteLine(ex.Message);
            }


Message has been deleted
Message has been deleted
Message has been deleted
Message has been deleted

Europass Interoperability

unread,
Apr 3, 2015, 2:23:03 AM4/3/15
to europass...@googlegroups.com
Hi Nelssen,

Thanks for your interest in Europass.

What do you mean exactly by "Download is in Blank"? 
Do you get a blank PDF file (e.g. just one blank page) or no file at all? 
If you do get a PDF file, can you please attach it here? 
Also, can you also please attach here the CV.xml file that you're using? (D:\\XML\\CV.xml)

The code you posted above is in C#, right? I'm not familiar with C# but most probably there's something wrong with your code since my Europass REST API tests run fine.

Best regards,

Dimitris Zavaliadis
Team Europass

nelssen....@gmail.com

unread,
Apr 3, 2015, 4:15:53 AM4/3/15
to europass...@googlegroups.com
Hello Dimitri,

You were right the problem was actually in the code!
Below you can find the working version of the code.
Thanks for you high availability and quick response.

Best Regards,
Nelssen

            string xmlContent = File.ReadAllText("D:\\XML\\CV.xml");
            
            HttpWebRequest request = (HttpWebRequest)WebRequest.Create(URL);
            request.Method = "POST";
            request.ContentType = "application/xml";
            using (Stream webStream = request.GetRequestStream())
            using (StreamWriter requestWriter = new StreamWriter(webStream, System.Text.Encoding.UTF8))
            {
                requestWriter.Write(xmlContent);
            }

            try
            {
                WebResponse webResponse = request.GetResponse();
                using (Stream webStream = webResponse.GetResponseStream())
                {
                    if (webStream != null)
                    {
                        using (StreamReader responseReader = new StreamReader(webStream))
                        {
                            if (webStream != null)
                            {
                                Response.Clear();
                                Response.Buffer = true;
                                Response.ContentType = "application/pdf";
                                Response.AddHeader("Content-Disposition", "attachment; filename=\"Test.pdf\"");
                                Response.Cache.SetCacheability(HttpCacheability.NoCache);
                                webStream.CopyTo(Response.OutputStream);
                                Response.Flush();

Europass Interoperability

unread,
Apr 3, 2015, 7:22:07 AM4/3/15
to europass...@googlegroups.com
Hi Nelssen,

Glad to here that you managed to fix the problem.

Please let us know if you need anything else.

Regards,

Dimitris Zavaliadis
Team Europass
Reply all
Reply to author
Forward
0 new messages