(Has all the HTML perfectly placed and the expected header and footer).
This is not a code problem because all our previous files and templates that was used before was successfully retrieved (and I also tested this on deployed code).
[ExcludeFromCodeCoverage]
public class SyncfusionWrapper : ISyncfusionWrapper
{
///
/// The path provider.
///
private readonly IPathProvider pathProvider;
///
/// Initializes a new instance of the class.
///
public SyncfusionWrapper(IPathProvider pathProvider)
{
this.pathProvider = pathProvider;
}
///
/// Converts the HTML files to PDF bytes.
///
///
The binary file path for
SyncFusion.
///
The HTML header file path.
///
The HTML footer file path.
/// The PDF bytes converted from HTML files.
public byte[] ConvertHtmlToPdfBytes(List htmlFiles, string webKitPath, string htmlHeaderPath = null, string htmlFooterPath = null)
{
byte[] result = null;
if (htmlFiles != null && htmlFiles.Count > 0)
{
var documents = new List
();
for (var ctr = 0; ctr < htmlFiles.Count; ctr++)
{
documents.Add(this.GetLoadedDocument(htmlFiles[ctr], webKitPath, htmlHeaderPath, htmlFooterPath));
if (ctr != 0)
{
//// FIRST DOCUMENT SHOULD EXTEND OTHER DOCUMENTS (IF ANY)
documents[0].ImportPageRange(documents[ctr], 0, documents[ctr].Pages.Count - 1);
}
}
var stream = documents[0].SaveAndGetStream();
result = stream.ToArray();
}
return result;
}
///
/// Gets the .
///
///
The binary file path for
SyncFusion.
///
The HTML header file path.
///
The HTML footer file path.
/// The created .
private PdfLoadedDocument GetLoadedDocument(string htmlFile, string webKitPath, string htmlHeaderPath = null, string htmlFooterPath = null)
{
var settings = new WebKitConverterSettings()
{
WebKitPath = webKitPath,
Margin = new PdfMargins() { Top = 40, Left = 30, Right = 40, Bottom = 50 },
};
if (!string.IsNullOrWhiteSpace(htmlHeaderPath))
{
settings.Margin.Top = 10;
settings.PdfHeader = this.GetHeaderFooterPdfPageTemplate(htmlHeaderPath, webKitPath);
}
if (!string.IsNullOrWhiteSpace(htmlFooterPath))
{
settings.Margin.Bottom = 10;
settings.PdfFooter = this.GetHeaderFooterPdfPageTemplate(htmlFooterPath, webKitPath);
}
var converter = settings.ToHtmlPdfConverter();
PdfDocument document = converter.Convert(htmlFile, string.Empty);
document.PageSettings.Size = PdfPageSize.A4;
var docStream = document.SaveAndGetStream();
var result = new PdfLoadedDocument(docStream);
return result;
}
///
/// Gets the header or footer PDF page template.
///
///
The binary file path for
SyncFusion.
/// The header or footer PDF page template.
private PdfPageTemplateElement GetHeaderFooterPdfPageTemplate(string htmlFilePath, string webKitPath)
{
var settings = new WebKitConverterSettings()
{
WebKitPath = webKitPath,
PdfPageSize = new SizeF(PdfPageSize.A4.Width, 20),
Orientation = PdfPageOrientation.Landscape,
SinglePageLayout = SinglePageLayout.FitWidth,
WebKitViewPort = new Size(1024, 0),
};
var converter = settings.ToHtmlPdfConverter();
var document = converter.Convert(htmlFilePath);
var firstPageSize = document.Pages[0].GetClientSize();
var bounds = new RectangleF(0, 0, firstPageSize.Width, firstPageSize.Height);
var header = new PdfPageTemplateElement(bounds);
header.Graphics.DrawPdfTemplate(document.Pages[0].CreateTemplate(), bounds.Location, bounds.Size);
return header;
}
}
I think that there is something wrong with the Convert method, because the other Lambda tool I used to test, using Nuget package Syncfusion.Pdf.Net.Core, worked on deployed AWS, here is code:
PdfDocument document = new PdfDocument();
//Add a page to the document
PdfPage page = document.Pages.Add();
//Create PDF graphics for the page
PdfGraphics graphics = page.Graphics;
//Set the standard font
PdfFont font = new PdfStandardFont(PdfFontFamily.Helvetica, 20);
//Draw the text
graphics.DrawString("Hello World!!!", font, PdfBrushes.Black, new PointF(0, 0));
string imagePath = Path.GetFullPath(@"Data/logo.jpg");
//Load the image from the disk
FileStream imageStream = new FileStream(imagePath, FileMode.Open, FileAccess.Read);
PdfBitmap image = new PdfBitmap(imageStream);
//Draw the image
graphics.DrawImage(image, 30, 30, 100, 25);
//Save the document into stream
MemoryStream stream = new MemoryStream();
//Save the PDF document
document.Save(stream);
document.Close();
File.WriteAllBytes("test.pdf", stream.ToArray());
System.Diagnostics.Process.Start(new System.Diagnostics.ProcessStartInfo("test.pdf") { UseShellExecute = true });
return Convert.ToBase64String(stream.ToArray());