I am trying to convert HTML Div to PDF in Asp.Net MVC. I have tried with iTextSharp and iText7 but the result is not as per my expectation. Div contains many images as base64 also after image there is some div which I have set with position over those image tags. I want PDF as shown in the below image.

However it's showing pdf like below :

Also I have HTML code is like below :
- "mainDiv" style="margin-top:-2.5%">class="droppable ui-droppable" style="margin-left:20%;">
"data:image/png;base64,iVBORw0KGgoAAAANSUhEUg...Base64 String">
class="ui-visual-focus textboxDraggable ui-draggable ui-draggable-handle ui-draggable-dragging ui-resizable" style="width: 291px; display: inline-block; position: absolute; z-index: 10; left: 469px; top: 245.438px; height: 26px;; font-family:Arial; font-size:14;" data-type="textbox" id="textbox_1" data-class="temp" onclick="clickMe(this)" data-name="FirstName">- Testing
class="ui-visual-focus textboxDraggable ui-draggable ui-draggable-handle ui-draggable-dragging ui-resizable" style="width: 281px; display: inline-block; z-index: 10; position: absolute; left: 470px; top: 325.797px; height: 33px;; font-family:Arial; font-size:14;" data-type="textbox" id="textbox_2" data-class="temp" onclick="clickMe(this)" data-name="LastName">- Testing Demo
And in controller side, I am passing div as string. And in the controller side my code is like below :
- [HttpPost, ValidateInput(false)]
- public FileResult HtmlToPdf(string GridHtml)
- {
- var workStream = new MemoryStream();
- using(var pdfWriter = new PdfWriter(workStream))
- {
- pdfWriter.SetCloseStream(false);
- using(var document = HtmlConverter.ConvertToDocument(GridHtml, pdfWriter))
- {}
- }
- workStream.Position = 0;
- return new FileStreamResult(workStream, "application/pdf");
- }
So draggable div is not placing on pdf with proper position. Which we need to achieve.
Marvin ReidPosted Aug 10, 2023, 8:33 PM
You can convert the HTML to PDF using the DocumentConverter. This will maintain the original positioning of your images. You can try the following code for this:
https://www.leadtools.com/help/sdk/tutorials/dotnet-console-convert-files-with-the-document-converter.html
Yashir KhanPosted Jun 18, 2020, 9:04 AM
I want to download that pdf on client side. so I am passing MemoryStream to Client side in json response. But on client side I am getting error like "Cannot access a closed Stream". By making R&D on google I found some solution and apply it in my code but it's not working. Same error we are getting every time. Please can you help me get out of this issue.
Thank you.
Jenith ThakkarPosted Jun 17, 2020, 12:17 AM