Reputation: 53
I want to show pictures in PDF with HtmlRenderer.PdfSharp. I am using this example. https://github.com/ArthurHub/HTML-Renderer
I can show this link in PDF. https://helpx.adobe.com/content/dam/help/en/stock/how-to/visual-reverse-image-search/jcr_content/main-pars/image/visual-reverse-image-search-v2_intro.jpg
But I can't show this link in PDF. https://images.data.gov.sg/api/traffic-images/2016/02/96128cfd-ab9a-4959-972e-a5e74bb149a9.jpg
Because when you enter this link, the picture is downloaded automatically and is not displayed in the browser.
This code generates the PDF.
private void OnGeneratePdf_Click(object sender, EventArgs e)
{
PdfGenerateConfig config = new PdfGenerateConfig();
config.PageSize = PageSize.A4;
config.SetMargins(20);
var doc = PdfGenerator.GeneratePdf(_mainControl.GetHtml(), config, null, DemoUtils.OnStylesheetLoad, HtmlRenderingHelper.OnImageLoadPdfSharp);
var tmpFile = Path.GetTempFileName();
tmpFile = Path.GetFileNameWithoutExtension(tmpFile) + ".pdf";
doc.Save(tmpFile);
Process.Start(tmpFile);
}
This is the error I get when pasting the link.
Upvotes: -1
Views: 688
Reputation: 53
That's how I solved the problem.
public static void GeneratePDF(string htmlstr)
{
PdfGenerateConfig config = new PdfGenerateConfig();
config.PageSize = PageSize.A4;
var doc = PdfGenerator.GeneratePdf(htmlstr, config, null, null, OnImageLoadPdfSharp);
var tmpFile = Path.GetTempFileName();
tmpFile = Path.GetFileNameWithoutExtension(tmpFile) + ".pdf";
doc.Save(tmpFile);
Process.Start(tmpFile);
}
public static void OnImageLoadPdfSharp(object sender, HtmlImageLoadEventArgs e)
{
using (var client = new WebClient())
{
var url = e.Src;
var tmpFile = Path.GetTempFileName();
client.DownloadFile(new Uri(url), tmpFile);
ResizeImage(tmpFile);
e.Callback(XImage.FromFile(tmpFile));
}
}
Upvotes: 0