I have a html content which has 100% width and covering the whole screen but when i render the same content in PDF it is just taking 50% width.
HtmlLoadOptions objLoadOptions = new HtmlLoadOptions();
objLoadOptions.PageInfo.IsLandscape = true;
MemoryStream memoryStream = new MemoryStream(Encoding.UTF8.GetBytes(htmlString));
Aspose.Pdf.Document doc = new Aspose.Pdf.Document(memoryStream, objLoadOptions);
memoryStream.Dispose();
doc.ProcessParagraphs();
double w, h = 0;
foreach (Page page in doc.Pages)
{
w = page.Rect.Width;
h = page.Rect.Height;
page.MediaBox = new Rectangle(0, 0, h * h / w, h);
page.CropBox = new Rectangle(0, 0, h * h / w, h);
}
this is the aspose logic i have written and this is the html content
The width of content in output PDF may not capture 100% area due to margins. API adds margins of 72 points around the page by default while generating PDF. You can remove margins by using following code snippet:
objLoadOptions.PageInfo.Margin = new MarginInfo(0, 0, 0, 0);
We hope this will resolve your issue. In case you still face any issue, please share complete HTML content with us along with output PDF. We will test the scenario in our environment and address it accordingly.
Thanks for the the reply. This still did not solve my issue.
This is the complete code with html content that i am using
HtmlLoadOptions objLoadOptions = new HtmlLoadOptions();
objLoadOptions.PageInfo.Margin = new MarginInfo(0, 0, 0, 0);
objLoadOptions.PageInfo.IsLandscape = true;
MemoryStream memoryStream = new MemoryStream(Encoding.UTF8.GetBytes(htmlString));
Aspose.Pdf.Document doc = new Aspose.Pdf.Document(memoryStream, objLoadOptions);
memoryStream.Dispose();
doc.ProcessParagraphs();
double w, h = 0;
foreach (Page page in doc.Pages)
{
w = page.Rect.Width;
h = page.Rect.Height;
page.MediaBox = new Rectangle(0, 0, h * h / w, h);
page.CropBox = new Rectangle(0, 0, h * h / w, h);
}
MemoryStream outputStream = new MemoryStream();
doc.Save(outputStream, SaveFormat.Pdf);
return new FileStreamResult(outputStream, "application/pdf");
Thanks for the reply. can you please tell me what ‘datadir’ means? and also i have to return back the memory stream to the calling function and there i will save that memory stream to my storage location. Can you please tell me what is missing in the code that i have written or can you please try to modify the same code that i have written (because the content of the document may have multiple pages and that is dynamic). would be great if you can tell me how to return back the memory stream back in the same way that i have sent.
If HTML content is using some resources e.g. images or files which are located somewhere, you can specify there full path in HtmlLoadOption(). In your case, it is not necessary so you can use HtmlLoadOptions with default/empty constructor.
You can definitely use memory stream as per your requirements. You need to remove only following code lines from your code snippet which you were using to set MediaBox. Due to which extra white space was adding in the PDF.
double w, h = 0;
foreach (Page page in doc.Pages)
{
w = page.Rect.Width;
h = page.Rect.Height;
page.MediaBox = new Rectangle(0, 0, h * h / w, h);
page.CropBox = new Rectangle(0, 0, h * h / w, h);
}
Please try after removing above code snippet from the code which you had shared earlier. In case you still face any issue, please feel free to let us know.
It is good to know that your issue has been resolved by using suggested approach. Please keep using our API and in case you need further assistance, please feel free to let us know.
Hope you are doing well, we are also facing the same issue but I tried to use your suggestion for “PageInfo” but in Aspose.PDF for java in 20.2 object is not available so to resolve this width issue in generated PDF what we require to do.
Can you please suggest.
Would you kindly share some sample HTML file and output PDF document to show the issue that you are facing actually. We will test the scenario in our environment and share our feedback accordingly.
Thanks and lots I am playing now with Width and Height and margin for making it good.
I have one query in PDF how we can add page footer Content irrespective of the Page count in PDF, as we don’t know in some cases it would be single page in one cases it could be 2 pages and more, in all of this if i wanted to add footer content in the pdf how we can do that.
You can iterate through pages of the generated PDF (After it is generated from HTML) and add text stamps at the bottom of each page as per your requirements. Please visit following documentation article for more details:
Thanks @asad.ali for your reference but it’s showing if it’s a simple text then we can do that what it’s stated , but suppose that footer having image with some specific style then how we can archive this .
PFA attachment of the HTML file without footer and generated PDF with image folder.
below is the code snippet we are using now
===============
// Specify the The base path/url for the html file which serves as images database
String basePath = “E:\PDFs\PDFs_view\PDFs\” ;
HtmlLoadOptions htmloptions = new HtmlLoadOptions(basePath);
// set MarginInfo for margin details
htmloptions.getPageInfo().setWidth(650); // in points 72pt = 1 inch
htmloptions.getPageInfo().setHeight(1200); // in points 72pt = 1 inch
htmloptions.getPageInfo().setMargin(new MarginInfo(20,20,20,20));
// Load HTML file
Document doc = new Document(basePath + "KPMG-and-Flexera-Withoutfooter.html", htmloptions);
// Save HTML file
doc.save(basePath+"KPMG-and-Flexera-Withoutfooter650-1200_mr20.pdf");
System.out.println("PDF Genertion done");
===============
**PDFs_Footer.zip (268.0 KB)
** below is the content which I wanted to append in every Page footer, how we can achive this.
+++++++++++++++++++++++++++++++++++++++++++++++++++++++
Thanks @asad.ali I have implemented the code it’s working for me also.
but thing is second paragraph is not printed in the generated PDF in footer section, I am also not able to download the pdf output which you have attached in the response , it’s giving me “Sorry, this file is private. Only visible to topic owner and staff members.” error.