-1

I'm trying to download through webclient a byte[] of a PDF rendered in the page.

This is the URL.

The problem i encounter is that the page above is not a direct page to the PDF, but a page which generates and render a PDF.

If I use my code:

WebClient labelDL = new WebClient();
// Récupération du fichier PDF

Content = labelDL.DownloadData(_labelSLSResponse.LabelUrl);

I get the byte[] im waiting for but it's the byte[] of the html content (which I dont even care).

How can I first render the url (like in a browser, but programmaticaly) and after it's generated then download the byte[] of the PDF which is rendered ?

Thanks in advance for your help, am stuck there...

Uwe Keim
  • 39,551
  • 56
  • 175
  • 291
  • 1
    When I `GET` that URL, I get a PDF right away, no HTML at all. – Bart Friederichs Feb 08 '17 at 08:26
  • There are tools like Aspose that render PDFs to an image. Or there is e.g. [jsPDF](https://github.com/MrRio/jsPDF) that renders PDF to HTML. – Uwe Keim Feb 08 '17 at 08:29
  • That's what am explaining. The url generates a pdf. When you use webclient on the url, you get the html first ! I already have tools to render a pdf into what i want, that's not my question but thanks – gstreetspirit Feb 08 '17 at 08:35
  • Hey gstreetspirit welcome to stackoverflow community . check the answer out it is a possible duplicate please make sure to do research before . – Sean Ch Feb 08 '17 at 08:40
  • It is not a duplicate question. My question is not "how to download byte[] of a pdf", but how to first render the page, and then, download the pdf generated by that page... – gstreetspirit Feb 08 '17 at 08:52
  • see the updated answer . you plug in the generated url and it will download the pdf file for you – Sean Ch Feb 08 '17 at 08:53
  • That still doesnt answer my question. In your update you provide the url of a direct PDF. The url I Have is an aspx page that, in the server side, generates and THEN renders a pdf in the browser. It's not a direct url to a PDF !! – gstreetspirit Feb 08 '17 at 09:00
  • fine then search little bit more . – Sean Ch Feb 08 '17 at 09:02

1 Answers1

0

I finally found the solution.

In fact the problem is linked to url format. It's a rewrited url. So to be interpreted correctly by webclient, you have to force headers (user-agent) as if you were a web browser.

with this code :

WebClient labelDL = new WebClient();
        // Récupération du fichier PDF

        labelDL.Headers.Add("user-agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.2; .NET CLR 1.0.3705;)");
        //string test = labelDL.DownloadString(_labelSLSResponse.LabelUrl);
        Content = labelDL.DownloadData(_labelSLSResponse.LabelUrl);

It finally works !

Thanks for your time and your help...