I am evaluating your component for converting HTML to pdf and I require ability to control the page sizes after the HTML has been imported into Aspose. Although an example is not necessary for my question, I would illustrate one scenario where this is needed. Refer to the attached html file and the 3rd page, if this is converted to PDF the 3rd page’s table will be cut off under normal settings.
Hi Kuna,
any updates?
we have tried many alternatives and seem to have run against walls (bugs or APIs not working as we would expect). Any direction on how to resize certain pages and have the converted HTML content reflow, would be appreciated!
Hi Kuna,
We are sorry for the inconvenience. I am afraid we can not set width/height of resultant PDF document less than minimal width/height of a html page during HTML to PDF conversion. However as a workaround we can resize the contents of resultant PDF document using ResizeContents() method of PdfFileEditor class, you may resize some specific page or whole PDF document as following.
HtmlLoadOptions htmloptions = new HtmlLoadOptions();
// Load HTML file into a Document object
Document doc = new Document("aspose.source.html", htmloptions);
// Process paragraphs to ensure all content is adjusted properly
doc.processParagraphs();
// Create an array for page numbers to resize
int[] pageNumbers = new int[doc.getPages().size()];
for (int i = 0; i < doc.getPages().size(); i++) {
pageNumbers[i] = i + 1;
}
// Create PdfFileEditor and set resize parameters
PdfFileEditor pfe = new PdfFileEditor();
PdfFileEditor.ContentsResizeParameters resizeParams = PdfFileEditor.ContentsResizeParameters.pageResize(17 * 72, 11 * 72);
pfe.resizeContents(doc, pageNumbers, resizeParams);
// Save the resized document as PDF
doc.save("aspose.source.pdf");
Best Regards,
Hi,
quick update: there is an issue with the code we sent, in function “HasOverflowCotnent”
Hi Kuna,
it’s been almost a week. any updates on this?
Hi Kuna,
We are sorry for the delayed response. I have checked your shared sample code and noticed that you are not using the suggested solution so data is trimming in the resultant PDF document. For default conversion please do not set page margins/width and later resize the PDF document to desired page settings. please check following amended code snippet and sample output files attached, especially pdf_1_8x11.pdf. Hopefully it will help you to accomplish the task,
However if you still want to detect the page content overflow then please confirm so we will investigate the option accordingly.
Dim _htmlLoadOptions As New Aspose.Pdf.HtmlLoadOptions()
Dim _sb As New StringBuilder()
Dim _testPath As String = "C:\Users\Home\Downloads\test (1)\Temp"
' Setup load options
' _htmlLoadOptions.PageInfo.Margin.Right = 20
' _htmlLoadOptions.PageInfo.Margin.Left = 20
' _htmlLoadOptions.PageInfo.Margin.Top = 20
' _htmlLoadOptions.PageInfo.Margin.Bottom = 20
' Test 1: Default conversion
Dim _pdf As New Aspose.Pdf.Document(_testPath & "test2.html", _htmlLoadOptions)
_pdf.Save(_testPath & "pdf_default.pdf", Aspose.Pdf.SaveFormat.Pdf)
_sb.AppendLine("Test 1: " & HasOverflowContent(_pdf))
' Test 1a: Resize to 8x11
_pdf = New Aspose.Pdf.Document(_testPath & "test2.html", _htmlLoadOptions)
ResizePagesInPlace(_pdf, 8, 11)
_pdf.Save(_testPath & "pdf_1_8x11.pdf", Aspose.Pdf.SaveFormat.Pdf)
_sb.AppendLine("Test 1a: " & HasOverflowContent(_pdf))
' Test 2: Resize to 11x17
_pdf = New Aspose.Pdf.Document(_testPath & "pdf_1_8x11.pdf")
ResizePagesInPlace(_pdf, 11, 17)
_pdf.Save(_testPath & "pdf_1_11x17.pdf", Aspose.Pdf.SaveFormat.Pdf)
_sb.AppendLine("Test 2: " & HasOverflowContent(_pdf))
' Additional logic...
We are sorry for the inconvenience.
Best Regards,
Thanks for your reply.
Hi Kuna,
Hi Kuna,
Hi Kuna,
ksubramaniyam@dymaxium.com:
Thanks for your reply.Now I do understand why your original solution wasn’t working for us. But this solution is not feasible for us for number of reasons as illustrated below. We believe this to be a bug anyway as we do not see why setting margins would impact the conversion process.This doesn’t work for us for the following reasons:1) We need to be able to control margins.2) Page resizing after the conversion is using scaling and things get either stretched or skewed. Not acceptable to us. (Initial page size ends up being 12.21x11.69. Odd size that doesn’t match any paper size and this varies based on content - again not a good thing for PDfs that need to be printed)3) We need to convert 300 page+ documents into PDF and only one or two pages may contain large tables. With this solution, we will always be converting all the pages at the highest width required.With #2, the end result is usually not acceptable from presentation/readability point of view.In any event, there is no need for us to convert all content at a higher page size when 98% of our pages would fit very well on 8.5x11 and would look better in this setting.Our solution to overcome the shortcoming is as follows:1) Start with 8.5x11 page size2) Convert each Section of the HTML to PDF3) Check if the section has overflow content.4) Set the page size to higher5) Repeat step 3-4 for the section till there is no overflow6) Stitch together/merge all the sectional PDFs into one PDF.This ensures that all the HTML is converted to 8.5x11 size and only the sections that overflow get converted at a higher page size. This allows us to convert the pages naturally to a proper paper size that can be printed. This seems a better compromise for us so we can maintain better 1-1 conversion quality (and avoids scaling/distortion issues).It all works well for us as we had mentioned earlier but the “HasOverflowContent” doesn’t work in all cases. It does a decent job with about 80-90% of the cases we have tested but fails in cases like the one we outlined in the code above.Getting passed your solution above, is there a solution to detect content overflows better? Can you suggest how we can fix our HasOverflowContent function?Let’s stick to answering this question with our “HasOverflowContent”, if you can suggest a solution to make it work 100%, we can work out the rest of the logic to convert properly on our end.
Can you let me know if you have any updates?
Hi Kuna,