尝试进行生成pdf的调用,返回字节。尽管谷歌持续了好几个小时,但我仍然得到404并且无法纠正这种情况。我尝试了很多不同的东西,这有点乱。感谢。
Global.asax中
RouteTable.Routes.MapHttpRoute(
name: "PdfApi",
routeTemplate: "global/api/pdf/{controller}",
defaults: new { id = RouteParameter.Optional });
致电
using (var client = new HttpClient() { BaseAddress = new Uri(this.BaseAddress + "/global/api") })
{
//client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/x-www-form-urlencoded"));
//var response = client.GetAsync(UrlParameters).Result;
var x = new List<KeyValuePair<string, string>>();
foreach(string key in this.QueryString.Keys)
x.Add(new KeyValuePair<string,string>(key, this.QueryString[key]));
var response = client.PostAsync("/pdf/GeneratePdf", new FormUrlEncodedContent(x)).Result;
return response.IsSuccessStatusCode ? response.Content.ReadAsAsync<byte[]>().Result : null;
}
阿比
//[RoutePrefix("global/api/pdf")]
public class PdfController : ApiController
{
public class Pdf
{
public bool BreakImage { get; set; }
public bool BreakText { get; set; }
public string Name { get; set; }
public string SourceUrl { get; set; }
}
[HttpPost]
//[Route("generate")]
public byte[] GeneratePdf([FromBody] string x)
{
var pdf = new Pdf();
string html = string.Empty;
using (var client = new System.Net.WebClient())
{
html = client.DownloadString(pdf.SourceUrl + "?pdf-printable=true");
}
try
{
var pdfConverter = new ExpertPdf.HtmlToPdf.PdfConverter()
{
AvoidImageBreak = !pdf.BreakImage,
AvoidTextBreak = !pdf.BreakText,
LicenseKey = WebConfigurationManager.AppSettings["_PdfConverterLicenseKey"],
UseGlyphs = true,
UsePrintCSSMediaType = true
};
//return pdfConverter.GetPdfBytesFromHtmlString(html, new Uri(url).GetLeftPart(UriPartial.Authority));
return pdfConverter.GetPdfBytesFromUrl(pdf.SourceUrl);
}
catch (Exception)
{
return null;
}
}
}