Trouble downloading PhantomJS generated pdf

2020-07-17 16:15发布

问题:

I am having some trouble downloading a PhantomJS generated file to my asp.net application. I am running phantomJs as a server: The file is generated correctly and saved to disk in the PhantomJS folder, but something happens during the transfer and inclusion into my Http response stream. The file is downloaded by the browser, but when I try to open it, the file errors with the message that it can't be opened. I suspect it gets corrupted in the stream transfer?

I would like to avoid reading the pdf from its stored location in the file system, and instead get it out of the response returned from PhantomJS

PhantomJS code:

page.open(url, function (status) {

    page.render(fullFileName);

    var fs = require('fs');

    var pdfContent = fs.read(fullFileName);
    response.statusCode = 200;
    response.headers = {
                    'Cache': 'no-cache',
                    'Content-Type': 'application/pdf',
                    'Connection': 'Keep-Alive',
                    'Content-Length': pdfContent.length
                    };

     response.setEncoding("binary");
     response.write(pdfContent);


});

ASP.NET code:

public ActionResult DownloadPdfFromUrl(ConvertionModel model)
    {
        HttpWebRequest httpWReq = (HttpWebRequest)WebRequest.Create("http://localhost:8080");

        ASCIIEncoding encoding = new ASCIIEncoding();
        string postData = string.Format("url={0}&fileName=myTest&extension=pdf&format=pdf", model.UrlToConvertPdf);
        byte[] data = encoding.GetBytes(postData);

        httpWReq.Method = "POST";
        httpWReq.ContentType = "application/x-www-form-urlencoded";
        httpWReq.ContentLength = data.Length;

        using (Stream s = httpWReq.GetRequestStream())
        {
            s.Write(data, 0, data.Length);
        }

        var response = (HttpWebResponse)httpWReq.GetResponse();

        Response.AppendHeader("Content-Disposition", "attachment;filename=test.pdf");

        return new FileStreamResult(response.GetResponseStream(), "application/pdf");
    }

回答1:

The C# code looks good, but it's better not to return null action result. I'd better write

var stream = response.GetResponseStream();

var buffer = new byte[response.ContentLength];
stream.Read(buffer, 0, buffer.Length);

return File(buffer, "application/pdf", "test.pdf");

Also set response encoding in PhantomJS before writing document body:

response.setEncoding("binary");
response.write(pdfContent);