Hi Team, I am making Lambda function to generate pdf using blinkconverterSettings and HtmlToPdfConverter and I am using Syncfusion.HtmlToPdfConverter.Net.Aws package for this. In the generated pdf I am using cover Page as the first page and I donot want to include header and footer in coverPage and also the page numbers should exclude the counting of Cover Page.
So how can I achieve this.
Below I have shared the lambda function code.
We can skip the header and footer for first page during HTML to PDF conversion.
Please find the below KB link
You can add cover page to the PDF using below highlighted code snippet
|
HtmlToPdfConverter htmlConverter = new HtmlToPdfConverter(); //Convert HTML to PDF document PdfDocument document = htmlConverter.Convert(files[0]); //Add cover page PdfPage coverPage = new PdfPage(); //Insert page document.Pages.Insert(0, coverPage); //Draw text in cover page PdfFont font = new PdfTrueTypeFont(new Font("Helvetica", 16, FontStyle.Bold), true); PdfSolidBrush brush = new PdfSolidBrush(Color.FromArgb(44, 71, 120)); coverPage.Graphics.DrawString("cover page", font, brush, new PointF()); |
Please find the below KB link
You can use either one of the approaches based on your requirements.
Hi Team,
Thanks for your quick response.
I have implemented as per your suggestion by following this link: https://support.syncfusion.com/kb/article/7897/skip-header-and-footer-for-first-page-during-html-to-pdf-conversion
But now the after generating pdf, it seems like the pages are overriding to each other as there seems the licensing message twice on cover page and also the text on rest of the pages goes rightward after implemented this.
So kindly tell me how can I resolve this issue.
Also I want that the page number should start from second page and the total number of pages should not include the cover page. So how can I implement this too.
I have attached the source code and the sample pdf generated below.
Thanks and Regards
Harshit Goel
1.To resolve the issue of the License watermark being added twice, you will need to register the license key. This problem occurs because the license message is added once during the HTML To PDF conversion process and then it is added again when drawing the template on the PDF document
2.To exclude the cover page from the page count, you can insert the cover page after adding the header and footer to the document. By doing so, the cover page will not be included in the numbering sequence of the document's pages. And we have attached the output for your reference.
Output: https://www.syncfusion.com/downloads/support/directtrac/general/ze/Output-120236788
Please find the below code snippet
|
public static void HTMLToPDF() { Syncfusion.Licensing.SyncfusionLicenseProvider.RegisterLicense("Your key"); HtmlToPdfConverter htmlConverter = new HtmlToPdfConverter();
BlinkConverterSettings blinkConverterSettings = new BlinkConverterSettings();
//Table of content property. Set it as true blinkConverterSettings.EnableToc = true;
//Page size - A4 (8.27 * 11.69 inches) using 72 Twips per inch blinkConverterSettings.PdfPageSize = new Syncfusion.Drawing.SizeF(595, 842);
//Set PDF page orientation blinkConverterSettings.Orientation = PdfPageOrientation.Portrait;
//Add margins - 1 INCH using 72 Twips per inch blinkConverterSettings.Margin.Top = 50; blinkConverterSettings.Margin.Bottom = 50; blinkConverterSettings.Margin.Right = 40; blinkConverterSettings.Margin.Left = 40;
blinkConverterSettings.Scale = 0.6f; //set the content size in pdf
//Set Blink viewport size blinkConverterSettings.ViewPortSize = new Syncfusion.Drawing.Size(523, 770);
//Assign Blink converter settings to HTML converter. htmlConverter.ConverterSettings = blinkConverterSettings;
htmlConverter.ConverterSettings.EnableHyperLink = true;
string html = Path.GetFullPath("../../../Data/HtmlSample.html"); //Convert HTML to PDF. PdfDocument document = htmlConverter.Convert(html);
//Create a new instance of PdfDocument PdfDocument coverPageDocument = new PdfDocument();
//Add cover page HTML HtmlToPdfConverter coverPageHtmlConverter = new HtmlToPdfConverter(HtmlRenderingEngine.Blink); string cbaseUrl = @""; coverPageDocument = coverPageHtmlConverter.Convert("<div id='page1' style='text-align: center;'><p style='padding-top: 500px; font-size: 52px;'>Emergency Operations Plan</p1><p style='font-size: 34px; '>BowMac University</p><p style='font-size: 20px; line-height: 2.0; padding-top: 400px; '><strong> 10 Ontario Street,<br/>Honeoye Falls, New York, NY 14472<br/>585-624-9595<br/><a>www.bowmac.com</a></strong></p><p style='font-size: 18px; padding-top: 50px;>(C) 1999 - 2022 BLUELINE Security Consulting</p></div>", cbaseUrl); PdfPage coverPage = coverPageDocument.Pages[0];
PdfDocument pdfDocument = new PdfDocument();
for (int i = 0; i < document.Pages.Count; i++) { //Get loaded page as template Syncfusion.Pdf.Graphics.PdfTemplate template = document.Pages[i].CreateTemplate();
//Create new page PdfPage page = pdfDocument.Pages.Add();
//Draw template with the size as loaded page size page.Graphics.DrawPdfTemplate(template, Syncfusion.Drawing.PointF.Empty, page.GetClientSize()); } //Save the document into stream. MemoryStream memoryStream = new MemoryStream();
//Save and Close the pdfDocument. pdfDocument.Save(memoryStream);
//blinkConverterSettings.PdfFooter = CreateFooter("Template_EOP_ABCUniversiy2" PdfDocument document1 = HeaderFooter(memoryStream, 1, "Date Issued: Draft", "eopOwner", "Template_EOP_ABCUniversiy2");
// Save the modified document to a memory stream MemoryStream modifiedStream = new MemoryStream(); document1.Save(modifiedStream); File.WriteAllBytes("HeaderFooter.pdf", modifiedStream.ToArray());
PdfLoadedDocument loadedDocument = new PdfLoadedDocument(modifiedStream); loadedDocument.Pages.Insert(0, coverPage); MemoryStream stream= new MemoryStream(); loadedDocument.Save(stream); File.WriteAllBytes("Output.pdf",stream.ToArray());
} private static PdfDocument HeaderFooter(MemoryStream stream, float versionNumber, string generateMode, string eopOwner, string eopName) { //Load the existing PDF document PdfLoadedDocument loadedDocument = new PdfLoadedDocument(stream);
//Create a new PDF document PdfDocument document = new PdfDocument(); document.ImportPageRange(loadedDocument, 0, loadedDocument.Pages.Count - 1); //Add the page document.Template.Top = CreatePdfHeader(document, "Version Number: " + versionNumber, generateMode);
//Include the footer document.Template.Bottom = CreatePdfFooter(document, "Template_" + eopName);
return document; }
private static PdfPageTemplateElement CreatePdfHeader(PdfDocument doc, string title, string generateMode) { RectangleF rect = new RectangleF(0, 0, doc.Pages[0].GetClientSize().Width, 26); PdfPageTemplateElement header = new PdfPageTemplateElement(rect); header.Graphics.DrawRectangle(PdfBrushes.LightGray, rect); Syncfusion.Pdf.Graphics.PdfFont font = new Syncfusion.Pdf.Graphics.PdfStandardFont(Syncfusion.Pdf.Graphics.PdfFontFamily.Helvetica, 12); Syncfusion.Pdf.Graphics.PdfSolidBrush brush = new Syncfusion.Pdf.Graphics.PdfSolidBrush(Syncfusion.Drawing.Color.Navy); //Create title field PdfCompositeField compositeField; compositeField = new PdfCompositeField(font, brush, title); compositeField.Bounds = header.Bounds; compositeField.Draw(header.Graphics, new Syncfusion.Drawing.PointF(4, 7)); PdfCompositeField compositeField2; //Create date field PdfDateTimeField date = new PdfDateTimeField(font); if (generateMode == "Release") { date.DateFormatString = "MMM' 'dd', 'yyyy"; compositeField2 = new PdfCompositeField(font, brush, "Date Issued: {0}", date); compositeField2.Draw(header.Graphics, new Syncfusion.Drawing.PointF(375, 7)); } else { compositeField2 = new PdfCompositeField(font, brush, "Date Issued: Draft"); compositeField2.Draw(header.Graphics, new Syncfusion.Drawing.PointF(415, 7)); } compositeField2.Bounds = header.Bounds; return header; } //Create footer for PDF private static PdfPageTemplateElement CreatePdfFooter(PdfDocument doc, string footerText) { RectangleF rect = new RectangleF(0, 0, doc.Pages[0].GetClientSize().Width, 26); PdfPageTemplateElement footer = new PdfPageTemplateElement(rect); footer.Graphics.DrawRectangle(PdfBrushes.LightGray, rect); Syncfusion.Pdf.Graphics.PdfSolidBrush brush = new Syncfusion.Pdf.Graphics.PdfSolidBrush(Syncfusion.Drawing.Color.Navy); Syncfusion.Pdf.Graphics.PdfFont font = new Syncfusion.Pdf.Graphics.PdfStandardFont(Syncfusion.Pdf.Graphics.PdfFontFamily.Helvetica, 12); //Create footerText field PdfCompositeField compositeField2 = new PdfCompositeField(font, brush, footerText); compositeField2.Bounds = footer.Bounds; compositeField2.Draw(footer.Graphics, new Syncfusion.Drawing.PointF(4, 7)); //Create page count field PdfPageNumberField pageNumber = new PdfPageNumberField(font, brush); PdfPageCountField count = new PdfPageCountField(font, brush); PdfCompositeField compositeField = new PdfCompositeField(font, brush, "Page {0} of {1}", pageNumber, count); compositeField.Bounds = footer.Bounds; compositeField.Draw(footer.Graphics, new Syncfusion.Drawing.PointF(435, 7)); return footer; } |
Hi Team,
Thanks for your response.
I implemented similarly as you suggest and I achieved the requirement of removing header and footer from the coverPage of the Pdf and also the coverPage is now not including in page count as I want.
But after implementing the same, I found the issue that the Header and footer got stick to the corners(top, left, right, bottom) and the margin of the content is also increased to 0.5 inches more from all the sides itself although earlier, before implementing this everything was looking good as you can find the previous output in earlier attachment I shared.
Also I am sharing the code as well as latest output below:
We have checked the reported issue with the provided details,but it is working properly on our end.We have attached the sample and output for your reference. Kindly try the sample on your end and let us know the result.
Sample : https://www.syncfusion.com/downloads/support/directtrac/general/ze/Htmlnet6.0-1055257223
Output : https://www.syncfusion.com/downloads/support/directtrac/general/ze/Output397698725
If still you have facing the issue,we request you to share simplified sample,product version to check this issue on our end.So that it will be helpful for us to analyze and assist you further on this.
Hi Team,
With your provided sample, it is working fine. But in your sample you have used Syncfusion.HtmlToPdfConverter.Net.Windows package but in our project we're making lambda function and using Syncfusion.HtmlToPdfConverter.Net.Aws package.
We tried to generate Pdf using your sample with Syncfusion.HtmlToPdfConverter.Net.Aws package, but it throws the error:
'Blink files are missing at C:\Users\HARSHIT GOEL\Desktop\Htmlnet6.0\bin\Debuget6.0\runtimes\win-x64ative'.
Our code is working fine with Syncfusion.HtmlToPdfConverter.Net.Aws package but the issue that we reported earlier persists.
Thanks and Regards
Harshit Goel
Currently we are checking the reported issue on our end an will update the further details on july 13th,2023
We have checked the reported issue in windows local host and AWS lambda function but it working properly on our end.We have attached the code snippet and output for your reference.
Sample code: https://www.syncfusion.com/downloads/support/directtrac/general/ze/Function-1880328607
Output: https://www.syncfusion.com/downloads/support/directtrac/general/ze/Output1583552478
Kindly try the above sample on your end and revert us
Hi Team,
I had tried your sample on my end but pdf is not generating as per your provided sample output.
I have attached the source code containing Aws Lambda function, C# client project and the sample output generated at my end.
Packages we're using on server side:
Syncfusion.HtmlToPdfConverter.Net.Aws
Amazon.Lambda.Serialization.SystemTextJson
Amazon.Lambda.Core
Please help to resolve the issue as soon as possible.
We have created a new ticket under your account to follow up with this query. We suggest you to follow up with the ticket for further updates. Please login using the below link.
https://support.syncfusion.com/support
We have attached the sample for your reference. kindly try the sample on your end and let us know the result
Sample :
https://www.syncfusion.com/downloads/support/directtrac/general/ze/AWSLambdaFunction1031881706
https://www.syncfusion.com/downloads/support/directtrac/general/ze/HTML_To_PDF_Console673583836
Output :
https://www.syncfusion.com/downloads/support/directtrac/general/ze/Sample-1492716735
Please find the below UG documentation link
https://help.syncfusion.com/file-formats/pdf/convert-html-to-pdf/aws#aws-lambda