Как я могу открыть PDF в браузере вместо загрузки в WebAPI
Я использую selectPdf для создания динамических PDF-файлов в моем webapt. Выберите PDF. Загрузите этот PDF-файл как вложение. Я хочу, чтобы этот PDF-файл отображался на вкладке браузера вместо загрузки. ниже мой код.
[HttpGet]
[Route("myapplication")]
[AllowAnonymous]
private System.Net.Http.HttpResponseMessage GeneratePDF(string guid, bool isAr, out string fileName)
{
string htmlString = ApplicationService.GeneratePDFHTMLString(guid, isAr, out fileName);
string pdf_page_size = "A4";
PdfPageSize pageSize = (PdfPageSize)Enum.Parse(typeof(PdfPageSize),
pdf_page_size, true);
string pdf_orientation = "Portrait";
PdfPageOrientation pdfOrientation =
(PdfPageOrientation)Enum.Parse(typeof(PdfPageOrientation),
pdf_orientation, true);
int webPageWidth = 748;
int webPageHeight = 0;
// instantiate a html to pdf converter object
HtmlToPdf converter = new HtmlToPdf();
// set converter options
converter.Options.PdfPageSize = pageSize;
converter.Options.PdfPageOrientation = pdfOrientation;
converter.Options.WebPageWidth = webPageWidth;
converter.Options.MarginTop = 20;
converter.Options.WebPageHeight = webPageHeight;
converter.Options.ExternalLinksEnabled = true;
converter.Options.EmbedFonts = true;
converter.Options.KeepTextsTogether = true;
try
{
// create a new pdf document converting an url
PdfDocument doc = converter.ConvertHtmlString(htmlString);
doc.CompressionLevel = PdfCompressionLevel.NoCompression;
//var contents = doc.Save();
//doc.Close();
//return contents;
MemoryStream pdfStream = new MemoryStream();
// save pdf document into a MemoryStream
doc.Save(pdfStream);
// reset stream position
pdfStream.Position = 0;
System.Net.Http.HttpResponseMessage response = new System.Net.Http.HttpResponseMessage();
response.StatusCode = System.Net.HttpStatusCode.OK;
//response.Content = new System.Net.Http.StreamContent(pdfStream);
response.Content = new System.Net.Http.ByteArrayContent(pdfStream.ToArray());
response.Content.Headers.ContentDisposition = new System.Net.Http.Headers.ContentDispositionHeaderValue("attachment")
{
FileName = "foo.pdf"
};
response.Content.Headers.ContentType = new System.Net.Http.Headers.MediaTypeHeaderValue("application/octet-stream");
doc.Close();
return response;
}
catch (Exception ex)
{
throw ex;
}
}
0 ответов
[HttpGet]
[Route("myapplication")]
[AllowAnonymous]
private System.Net.Http.HttpResponseMessage GeneratePDF(string guid, bool isAr, out string fileName)
{
string htmlString = ApplicationService.GeneratePDFHTMLString(guid, isAr, out fileName);
string pdf_page_size = "A4";
PdfPageSize pageSize = (PdfPageSize)Enum.Parse(typeof(PdfPageSize),
pdf_page_size, true);
string pdf_orientation = "Portrait";
PdfPageOrientation pdfOrientation =
(PdfPageOrientation)Enum.Parse(typeof(PdfPageOrientation),
pdf_orientation, true);
int webPageWidth = 748;
int webPageHeight = 0;
// instantiate a html to pdf converter object
HtmlToPdf converter = new HtmlToPdf();
// set converter options
converter.Options.PdfPageSize = pageSize;
converter.Options.PdfPageOrientation = pdfOrientation;
converter.Options.WebPageWidth = webPageWidth;
converter.Options.MarginTop = 20;
converter.Options.WebPageHeight = webPageHeight;
converter.Options.ExternalLinksEnabled = true;
converter.Options.EmbedFonts = true;
converter.Options.KeepTextsTogether = true;
try
{
// create a new pdf document converting an url
PdfDocument doc = converter.ConvertHtmlString(htmlString);
doc.CompressionLevel = PdfCompressionLevel.NoCompression;
//var contents = doc.Save();
//doc.Close();
//return contents;
MemoryStream pdfStream = new MemoryStream();
// save pdf document into a MemoryStream
doc.Save(pdfStream);
// reset stream position
pdfStream.Position = 0;
System.Net.Http.HttpResponseMessage response = new System.Net.Http.HttpResponseMessage();
response.StatusCode = System.Net.HttpStatusCode.OK;
//response.Content = new System.Net.Http.StreamContent(pdfStream);
response.Content = new System.Net.Http.ByteArrayContent(pdfStream.ToArray());
//response.Content.Headers.ContentDisposition = new
// System.Net.Http.Headers.ContentDispositionHeaderValue("attachment")
// {
// FileName = "foo.pdf"
// };
response.Content.Headers.ContentType = new System.Net.Http.Headers.MediaTypeHeaderValue("application/pdf");
doc.Close();
return response;
}
catch (Exception ex)
{
throw ex;
}
}
Попробуй это