Spire.PDF is a professional PDF library applied to creating, writing, editing, handling and reading PDF files without any external dependencies. Get free and professional technical support for Spire.PDF for .NET, Java, Android, C++, Python.

Mon Feb 20, 2023 10:53 pm

Hi Support,

I am still trying to understand on how to convert HTML to PDF using your FreeSpire.PDF. But i am stucked with fact that my code is not producing pdf with the content,

Here is my c# code:

Code: Select all

using Spire.Pdf;
using Spire.Pdf.Actions;
using Spire.Pdf.General;
using Spire.Pdf.Graphics;
using Spire.Pdf.HtmlConverter;
using System;
using System.Drawing;


namespace HygeianReports
{
    class Program
    {
        static void Main(string[] args)
        {
            PdfDocument doc = new PdfDocument();
            doc.PageSettings.Size = PdfPageSize.A4;
            doc.PageSettings.Margins = new PdfMargins(0);
            PdfMargins margins = new PdfMargins(60, 60, 60, 60);
            doc.Template.Top = CreateHeaderTemplate(doc, margins);
            doc.Template.Bottom = new PdfPageTemplateElement(doc.PageSettings.Size.Width, margins.Bottom);
            doc.Template.Left = new PdfPageTemplateElement(margins.Left, doc.PageSettings.Size.Height);
            doc.Template.Right = new PdfPageTemplateElement(margins.Right, doc.PageSettings.Size.Height);
            doc.LoadFromFile(@"C:\test.html", FileFormat.HTML);

            doc.SaveToFile(@"C:\PdfHeader.pdf");
        }
        static PdfPageTemplateElement CreateHeaderTemplate(PdfDocument doc, PdfMargins margins)
        {
            //get page size
            SizeF pageSize = doc.PageSettings.Size;

            //create a PdfPageTemplateElement object as header space
            PdfPageTemplateElement headerSpace = new PdfPageTemplateElement(pageSize.Width, margins.Top);
            headerSpace.Foreground = false;

            //declare two float variables
            float x = margins.Left;
            float y = 0;
            //draw image in header space
            PdfImage headerImage = PdfImage.FromFile(@"C:\logo.png");
            float width = headerImage.Width / 4;
            float height = headerImage.Height / 4;

            float image_y = 2;

            headerSpace.Graphics.DrawImage(headerImage, x, image_y, width, height);

            //draw line in header space
            PdfPen pen = new PdfPen(PdfBrushes.Gray, 1);
            headerSpace.Graphics.DrawLine(pen, x, y + margins.Top - 2, pageSize.Width - x, y + margins.Top - 2);

            //draw text in header space
            PdfTrueTypeFont font = new PdfTrueTypeFont(new Font("Impact", 25f, FontStyle.Bold));
            PdfStringFormat format = new PdfStringFormat(PdfTextAlignment.Left);
            String headerText = "Temperature Summary";
            SizeF size = font.MeasureString(headerText, format);
            headerSpace.Graphics.DrawString(headerText, font, PdfBrushes.Gray, pageSize.Width - x - size.Width - 2, margins.Top - (size.Height + 5), format);

            //return headerSpace
            return headerSpace;
        }
    }
}



and here is the content of my test.html

Code: Select all
<!DOCTYPE html>
<html lang="en">
<head>
  <title>How to Test HTML Code in a Browser - DZone</title>
</head>
<body>
<table style="width:100%;padding-left:10px;padding-right:10px;border:solid 1px #d9d9d9;">
   <tr>
       <td style="20%">
           <label>Sites in this Report</label>      
      </td>
      <td style="60%">test</td>
       <td style="20%;">
           <label>Report Generated</label>      
      </td>
   </tr>
   <tr>
       <td style="20%">
           <label>Sites in this Report</label>      
      </td>
      <td style="60%">&nbsp;</td>
       <td style="20%;">
           <label>Report Generated</label>      
      </td>
   </tr>   
</table>
</body>
</html>






I just wondering what i did wrong, and why the produced pdf doesn't has any content except the headers only. So where those HTML go?

Thank you

johnhadikusumo
 
Posts: 4
Joined: Mon Feb 20, 2023 10:47 pm

Tue Feb 21, 2023 3:47 am

Hello,

Thanks for your inquiry.
For your requirement, I modified your code according to your requirement, and I attached it below.
In addition, I suggest you using our latest version of Spire.Pdf 9.2.6 due to it more stable and fixes more bugs than free version. We are willing to provide a temporary license (one month free) to help you remove the warning message. If you need it, you can contact our sales team([email protected]). Meanwhile, in principle, we don’t provide technical support for customers using the free version.

Code: Select all
using Spire.Pdf.Graphics;
using Spire.Pdf;
using System;
using System.Drawing;
using Spire.Pdf.HtmlConverter;
using System.IO;
using System.Threading;

namespace HTMLpDF32475
{
    internal class Program
    {
        static void Main(string[] args)
        {
            PdfDocument doc = new PdfDocument();
            doc.PageSettings.Size = PdfPageSize.A4;
            doc.PageSettings.Margins = new PdfMargins(0);
            PdfMargins margins = new PdfMargins(60, 60, 60, 60);
            doc.Template.Top = CreateHeaderTemplate(doc, margins);
            doc.Template.Bottom = new PdfPageTemplateElement(doc.PageSettings.Size.Width, margins.Bottom);
            doc.Template.Left = new PdfPageTemplateElement(margins.Left, doc.PageSettings.Size.Height);
            doc.Template.Right = new PdfPageTemplateElement(margins.Right, doc.PageSettings.Size.Height);
           
            //Add new page
            doc.Pages.Add();

            //Add a temp pdfdocument for converting html to pdf
            PdfDocument temp = new PdfDocument();

            //Set the size of temp pdfdocument
            temp.PageSettings.Width = PdfPageSize.A4.Width - margins.Right - margins.Left;
            temp.PageSettings.Height = PdfPageSize.A4.Height - margins.Top - margins.Bottom;


            PdfHtmlLayoutFormat htmlLayoutFormat = new PdfHtmlLayoutFormat();
            htmlLayoutFormat.IsWaiting = false;

            String htmlpath = @"../../data/test.html";
            string source = File.ReadAllText(htmlpath);

            //Converting html to pdf, please note that the method of LoadFromHTML only run in in a single-threaded.
            Thread thread = new Thread(() =>
          { temp.LoadFromHTML(source, true, temp.PageSettings, htmlLayoutFormat); });

            thread.SetApartmentState(ApartmentState.STA);
            thread.Start();
            thread.Join();

            PdfPageBase pdfPageBase = temp.Pages[0];

            //Draw the temp pdfdocument as a template in doc
            pdfPageBase.CreateTemplate().Draw(doc.Pages[0], 0, 0);

             temp.Close();
           
            //Save the file
              doc.SaveToFile(@"../../output/PdfHeader.pdf",FileFormat.PDF);
          }

        static PdfPageTemplateElement CreateHeaderTemplate(PdfDocument doc, PdfMargins margins)
        {
            //get page size
            SizeF pageSize = doc.PageSettings.Size;

            //create a PdfPageTemplateElement object as header space
            PdfPageTemplateElement headerSpace = new PdfPageTemplateElement(pageSize.Width, margins.Top);
            headerSpace.Foreground = false;

            //declare two float variables
            float x = margins.Left;
            float y = 0;
            //draw image in header space
            PdfImage headerImage = PdfImage.FromFile(@"../../data/E-iceblueLogo.png");
            float width = headerImage.Width / 4;
            float height = headerImage.Height / 4;

            float image_y = 2;

            headerSpace.Graphics.DrawImage(headerImage, x, image_y, width, height);

            //draw line in header space
            PdfPen pen = new PdfPen(PdfBrushes.Gray, 1);
            headerSpace.Graphics.DrawLine(pen, x, y + margins.Top - 2, pageSize.Width - x, y + margins.Top - 2);

            //draw text in header space
            PdfTrueTypeFont font = new PdfTrueTypeFont(new Font("Impact", 25f, FontStyle.Bold));
            PdfStringFormat format = new PdfStringFormat(PdfTextAlignment.Left);
            String headerText = "Temperature Summary";
            SizeF size = font.MeasureString(headerText, format);
            headerSpace.Graphics.DrawString(headerText, font, PdfBrushes.Gray, pageSize.Width - x - size.Width - 2, margins.Top - (size.Height + 5), format);

            //return headerSpace
            return headerSpace;
        }
    }

}


Sincerely
Abel
E-iceblue support team
User avatar

Abel.He
 
Posts: 1010
Joined: Tue Mar 08, 2022 2:02 am

Tue Feb 28, 2023 7:41 am

thank you for your response. But the code on your replied raised another issues when i tried to run it.
It seems PdfHtmlLayoutFormat and LoadFromHTML is out of reference.


PdfHtmlLayoutFormat htmlLayoutFormat = new PdfHtmlLayoutFormat();
htmlLayoutFormat.IsWaiting = false;

PdfDocument temp = new PdfDocument();
temp.LoadFromHTML


i tried to fix the references:

using System;
using System.Drawing;
using Spire.Pdf.HtmlToPdf;
using Spire.Pdf.General;
using Spire.Pdf;
using Spire.Pdf.Tables;
using Spire.Pdf.HtmlConverter;
using Spire.Pdf.Graphics;

using System.IO;
using System.Threading;

but still those two object can't find their references. Could you give me a complete code including all the references of the imported library.



Note : i am using FreeSpire.PDF 8.6.0

johnhadikusumo
 
Posts: 4
Joined: Mon Feb 20, 2023 10:47 pm

Wed Mar 01, 2023 1:52 am

Hello,

Thanks for your feedback.
Please refer to the following reference code I used in my project (I aslo used FreeSpire.Pdf 8.6.0).

Code: Select all

using Spire.Pdf;
using Spire.Pdf.Graphics;
using Spire.Pdf.HtmlConverter;
using System;
using System.Drawing;
using System.IO;
using System.Threading;

namespace HTMLpDF32475
{
    internal class Program
    {
        static void Main(string[] args)
        {
            PdfDocument doc = new PdfDocument();
            doc.PageSettings.Size = PdfPageSize.A4;
            doc.PageSettings.Margins = new PdfMargins(0);
            PdfMargins margins = new PdfMargins(60, 60, 60, 60);
            doc.Template.Top = CreateHeaderTemplate(doc, margins);
            doc.Template.Bottom = new PdfPageTemplateElement(doc.PageSettings.Size.Width, margins.Bottom);
            doc.Template.Left = new PdfPageTemplateElement(margins.Left, doc.PageSettings.Size.Height);
            doc.Template.Right = new PdfPageTemplateElement(margins.Right, doc.PageSettings.Size.Height);
           
            //Add new page
            doc.Pages.Add();

            //Add a temp pdfdocument for converting html to pdf
            PdfDocument temp = new PdfDocument();

            //Set the size of temp pdfdocument
            temp.PageSettings.Width = PdfPageSize.A4.Width - margins.Right - margins.Left;
            temp.PageSettings.Height = PdfPageSize.A4.Height - margins.Top - margins.Bottom;


            PdfHtmlLayoutFormat htmlLayoutFormat = new PdfHtmlLayoutFormat();
            htmlLayoutFormat.IsWaiting = false;

            String htmlpath = @"../../data/test.html";
            string source = File.ReadAllText(htmlpath);

            //Converting html to pdf, please note that the method of LoadFromHTML only run in in a single-threaded.
            Thread thread = new Thread(() =>
          { temp.LoadFromHTML(source, true, temp.PageSettings, htmlLayoutFormat); });

            thread.SetApartmentState(ApartmentState.STA);
            thread.Start();
            thread.Join();

            PdfPageBase pdfPageBase = temp.Pages[0];

            //Draw the temp pdfdocument as a template in doc
            pdfPageBase.CreateTemplate().Draw(doc.Pages[0], 0, 0);

             temp.Close();
           
            //Save the file
              doc.SaveToFile(@"../../output/PdfHeader.pdf",FileFormat.PDF);
          }

        static PdfPageTemplateElement CreateHeaderTemplate(PdfDocument doc, PdfMargins margins)
        {
            //get page size
            SizeF pageSize = doc.PageSettings.Size;

            //create a PdfPageTemplateElement object as header space
            PdfPageTemplateElement headerSpace = new PdfPageTemplateElement(pageSize.Width, margins.Top);
            headerSpace.Foreground = false;

            //declare two float variables
            float x = margins.Left;
            float y = 0;
            //draw image in header space
            PdfImage headerImage = PdfImage.FromFile(@"../../data/E-iceblueLogo.png");
            float width = headerImage.Width / 4;
            float height = headerImage.Height / 4;

            float image_y = 2;

            headerSpace.Graphics.DrawImage(headerImage, x, image_y, width, height);

            //draw line in header space
            PdfPen pen = new PdfPen(PdfBrushes.Gray, 1);
            headerSpace.Graphics.DrawLine(pen, x, y + margins.Top - 2, pageSize.Width - x, y + margins.Top - 2);

            //draw text in header space
            PdfTrueTypeFont font = new PdfTrueTypeFont(new Font("Impact", 25f, FontStyle.Bold));
            PdfStringFormat format = new PdfStringFormat(PdfTextAlignment.Left);
            String headerText = "Temperature Summary";
            SizeF size = font.MeasureString(headerText, format);
            headerSpace.Graphics.DrawString(headerText, font, PdfBrushes.Gray, pageSize.Width - x - size.Width - 2, margins.Top - (size.Height + 5), format);

            //return headerSpace
            return headerSpace;
        }
    }

}


if you have any issue, just feel free to contact us.

Sincerely
Abel
E-iceblue support team
User avatar

Abel.He
 
Posts: 1010
Joined: Tue Mar 08, 2022 2:02 am

Wed Mar 01, 2023 11:43 pm

Image

I am still getting reference issues. Please the highlighted ones.


regards,

John

johnhadikusumo
 
Posts: 4
Joined: Mon Feb 20, 2023 10:47 pm

Thu Mar 02, 2023 6:12 am

Hello,

Thanks for your feedback.
I put my test project to our server, you can download it from the following link, if my test project doesn’t help you, please offer your simple test project that can reproduce your issue, you could upload your project via Dropbox or OneDrive server, then share us with the download link. Thanks for your assistance in advance.

https://www.e-iceblue.com/downloads/dem ... F32475.zip

Sincerely
Abel
E-iceblue support team
User avatar

Abel.He
 
Posts: 1010
Joined: Tue Mar 08, 2022 2:02 am

Return to Spire.PDF