PDFsharp & MigraDoc Foundation
https://forum.pdfsharp.net/

Generate PDF from HTML couldn't add backgrnd img from stream
https://forum.pdfsharp.net/viewtopic.php?f=2&t=4215
Page 1 of 1

Author:  kris [ Tue Dec 29, 2020 10:54 pm ]
Post subject:  Generate PDF from HTML couldn't add backgrnd img from stream

Hi! I have generated PDF from HTML using PDFSharp, all images are from stream

Code:
   
        var pngBinaryDataLogoLeft = File.ReadAllBytes(folderPath + "logo.png");
        var ImgDataURILogoLeft = @"data:image/png;base64," + Convert.ToBase64String(pngBinaryDataLogoLeft);
        html = html.Replace("[logoLeft]", ImgDataURILogoLeft);

        var pngBinaryDataBGImg = File.ReadAllBytes(folderPath + "background.png");
        var ImgDataURIBGImg = @"data:image/png;base64," + Convert.ToBase64String(pngBinaryDataBGImg);
        html = html.Replace("[BGImg]", ImgDataURIBGImg);

        var config = new PdfGenerateConfig();
        config.PageOrientation = PageOrientation.Landscape;
        config.PageSize = PdfSharpPageSize.A4;

        string cssStr = File.ReadAllText(folderPath + "1.css");
        CssData css = PdfGenerator.ParseStyleSheet(cssStr);

        PdfSharp.Pdf.PdfDocument pdf = PdfGenerator.GeneratePdf(html, config, css);

        MemoryStream stream = new MemoryStream();
        pdf.Save(stream, false);
        byte[] bytes = stream.ToArray();

        File.WriteAllBytes(folderPath + "document.pdf", bytes);


In my HTML the img get replaced by stream are working fine

Code:
<img class="logo-left" src="[logoLeft]" />



But the background image doesn't work:

Code:
<body style="background: url('[BGImg]')">



I don't have the image from a URL as it's from database (saved as stream)

I also tried to add the background image first to a new PDF page like below:

Code:
XGraphics gfx = XGraphics.FromPdfPage(pdf.Pages[0]);
XImage image = XImage.FromFile(bgImgPath);
gfx.DrawImage(image, x, y, width, height);


But then I couldn't add the content from HTML to the same page

If someone could please help thanks!!

Author:  () => true [ Mon Jan 04, 2021 9:12 am ]
Post subject:  Re: Generate PDF from HTML couldn't add backgrnd img from st

Read this: http://forum.pdfsharp.net/viewtopic.php?f=2&t=832

Author:  TH-Soft [ Mon Jan 04, 2021 9:14 am ]
Post subject:  Re: Generate PDF from HTML couldn't add backgrnd img from st

kris wrote:
If someone could please help thanks!!
You do not use PDFsharp, you use HTML Renderer. Not our product, but maybe someone here can help.

Page 1 of 1 All times are UTC
Powered by phpBB® Forum Software © phpBB Group
https://www.phpbb.com/