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%"> </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
