Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/mercurial/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 使用PDFDocument.Save将PDF另存为ByteArray到HttpResponse_C#_Pdf_Html To Pdf - Fatal编程技术网

C# 使用PDFDocument.Save将PDF另存为ByteArray到HttpResponse

C# 使用PDFDocument.Save将PDF另存为ByteArray到HttpResponse,c#,pdf,html-to-pdf,C#,Pdf,Html To Pdf,我正在尝试使用C#中的SelectPDF发送一个字节数组的转换-HTML到PDF。下面是我给出的示例代码。我有一个WebAPI,我在做这个。我需要以字节数组的形式发送PDF文档,并需要在客户端UI中显示。以下代码未执行 请参阅SelectPDF的文档 此外,用户界面代码对于通过搜索引擎访问此网站的任何人来说都是。您只需不传递重载,它就会返回一个字节[] doc.Save(); PDFDocument.Save()的签名是什么?它是否接受流?如果是这样的话,流很可能是用于输入,而不是输出。但我不

我正在尝试使用C#中的
SelectPDF
发送一个
字节数组
的转换-HTML到PDF。下面是我给出的示例代码。我有一个WebAPI,我在做这个。我需要以字节数组的形式发送PDF文档,并需要在客户端UI中显示。以下代码未执行

请参阅SelectPDF的文档


此外,用户界面代码对于通过搜索引擎访问此网站的任何人来说都是

。您只需不传递重载,它就会返回一个
字节[]

doc.Save();

PDFDocument.Save()的签名是什么?它是否接受
?如果是这样的话,
很可能是用于输入,而不是输出。但我不熟悉这个,我使用了iText。@GlennFerrie-请参考链接
,如果我将API返回类型设置为HttpResponseMessage,那么它就工作了,而不是返回HttpContext.Current.Response(返回类型为HttpResponse),它就不工作了。
请向我们展示正在工作的确切代码,和不起作用的代码,明白了。您的问题是最后一个参数应该是文件名,即Sample.pdf。这是下载时的文件名。-你不应该通过一条完整的路径。忘记我之前关于
流的评论吧
@GlennFerrie-我更新了这个问题。请参考包含工作代码的问题。
public HttpResponseMessage GetSamplePDF() {
    string html = "<html><body>hello world</body></html>";
    PdfDocument doc = converter.ConvertHtmlString(html, "");
    doc.Save("C:\MyProject\Pdf\Sample.pdf");

    pdfData = System.IO.File.ReadAllBytes("C:\MyProject\Pdf\Sample.pdf");

    var result = new HttpResponseMessage(HttpStatusCode.OK) {
                Content = new ByteArrayContent(reportdata)
            };
    result.Content.Headers.ContentDisposition =
                new System.Net.Http.Headers.ContentDispositionHeaderValue("attachment") {
                    FileName = "SamplePDF"
                };
    result.Content.Headers.ContentType =
                new MediaTypeHeaderValue("application/octet-stream");

   return result;
}
doc.Save();