Hello,
Product: Aspose.PDF for .NET
Version: latest (22.11), same issue with older versions.
We have the issue with images when converting HTML to PDF.
HTML are taken from received emails. When converting to PDF, larger images that are between pages are half cut-off and is not visible on second page. This happens always when the image is at the end of page and it does not fit to the page. We expect that such images that does not fit will be moved to next page OR image is split between two pages.
Screenshot from converted PDF:
1.PNG (58.7 KB)
Screenshot from email:
2.PNG (102.5 KB)
Result document:
RESULT.pdf (3.3 MB)
HTML input:
input.zip (3.9 KB)
Conversion code:
MemoryStream licenseStream = new MemoryStream(Properties.Resources.AsposeLicense);
Aspose.Pdf.License licensePdf = new Aspose.Pdf.License();
licensePdf.SetLicense(licenseStream);
MemoryStream pdfStream = new MemoryStream();
using (MemoryStream htmlStream = new MemoryStream(Encoding.UTF8.GetBytes(htmlDocument)))
{
HtmlLoadOptions options = new HtmlLoadOptions();
options.PageInfo.Width = 550;
options.PageInfo.Margin = new MarginInfo(0, 0, 0, 0);
Document pdfDocument = new Document(htmlStream, options);
pdfDocument.Save(pdfStream);
return pdfStream;
}
Is there any solution for this? Or is there an Aspose bug?
Thank you, Martin.
@martin27
If it suits you, you can use options.IsRenderToSinglePage = true;
property to render all HTML content on a single page so that images would not cut off. Please let us know in case you do not like this solution. We will further proceed accordingly.
We have used previously IsRenderToSinglePage = true property and it worked perfectly.
But our requirements have changed and our PDF documents must be divided into pages.
This is due to difficulty for our users adjusting settings for printing in the different PDF readers / different printers to print the PDF correctly on multiple pages.
@martin27
We have logged an issue as PDFNET-53168 in our issue tracking system for further investigation. We will look into its details and keep you posted with the status of its correction. Please be patient and spare us some time.
We are sorry for the inconvenience.
1 Like
Thank you for the logged issue ticket.
Meanwhile, I’m trying to implement a workaround for the issue - convert HTML to PDF as single page using this code options.IsRenderToSinglePage = true; and then divide into pages. It’s working, but there is another problem - texts between pages are cut-off.
See image: result.png (10.9 KB)
Example code:
MemoryStream licenseStream = new MemoryStream(Properties.Resources.AsposeLicense);
Aspose.Pdf.License licensePdf = new Aspose.Pdf.License();
licensePdf.SetLicense(licenseStream);
MemoryStream pdfStream = new MemoryStream();
using (MemoryStream htmlStream = new MemoryStream(Encoding.UTF8.GetBytes(htmlDocument)))
{
HtmlLoadOptions options = new HtmlLoadOptions();
options.PageInfo.Width = 550;
options.PageInfo.Margin = new MarginInfo(0, 0, 0, 0);
Document pdfDocument = new Document(htmlStream, options);
Page firstPage = pdfDocument.Pages[1];
double height = firstPage.Rect.Height;
double width = firstPage.Rect.Width;
double maxPageHeight = 11.7 * 72; // A4 height is 11.7 inches, 72 is units per inch
// 1: Copy the pages
int pagesCount = (int)Math.Ceiling(height / maxPageHeight);
for (int i = 1; i <= pagesCount - 1; i++)
{
int pageNumber = i + 1;
pdfDocument.Pages.Insert(pageNumber, firstPage);
}
// 2: Crop the pages
double leftCornerHeightCounter = height;
double rightCornerHeightCounter = height;
foreach (Page page in pdfDocument.Pages)
{
leftCornerHeightCounter = leftCornerHeightCounter - maxPageHeight;
Aspose.Pdf.Rectangle pageRect = new Aspose.Pdf.Rectangle(0, leftCornerHeightCounter, width, rightCornerHeightCounter);
rightCornerHeightCounter = rightCornerHeightCounter - maxPageHeight;
page.CropBox = pageRect;
page.MediaBox = pageRect;
}
pdfDocument.Save(pdfStream);
return pdfStream;
}
Is there any way to solve the issue?
Thank you,
Martin
@martin27
It is a custom scenario where you are trying to split a single page PDF document into multi-page file. The issue that you are experiencing is expected and regretfully, we do not have any workaround in order to prevent it. This information however has been logged under the same ticket ID for investigation. We will let you know once the ticket is resolved. Please spare us some time.
We are sorry for the inconvenience.