Create PDF from HTML string or file

Hi,

I am a new user. Trying to use the ASPOSE PDF products to generate the PDF from HTML.

I initially tried with the logic to generate a PDF from HTML string which didn’t work.

The code sample below

Document pdfDocument = new Document();
Page page = pdfDocument.Pages.Add();
page.PageInfo.IsLandscape = orientation == “landscape”;
page.PageInfo.Margin = new MarginInfo(0, 0, 0, 0);

            page.Paragraphs.Add(new HtmlFragment(data));
            
            var outputStream = new MemoryStream();
            outputStream.Seek(0, SeekOrigin.Begin);
            pdfDocument.Save(outputStream);

The changes works in local setup but when I deploy to my ECS container, I am getting NULL reference exceptions like below

     at Aspose.Pdf.Document.#=z34X_fbg=(Stream #=zqBLBFAY=, LoadOptions #=zeQ3xAgI=)
     at #=zU9Wnb1U_5_LqLGwK1GbVvuNrcAJ2.#=zKQhVxb0=(Stream #=zWEEOlT1bNO4U, Document #=z9Bi9GFQ1xlFo, HtmlLoadOptions #=zLg7IsLM1gg0Q)
     at #=zU9Wnb1U_5_LqLGwK1GbVvuNrcAJ2.#=zPS0xp0dRgFcd(Stream #=zWEEOlT1bNO4U, Document #=z9Bi9GFQ1xlFo, HtmlLoadOptions #=zLg7IsLM1gg0Q, String #=zXG$5A_CGQ3wO)

So as an alternative approach, I tried to generate the HTML file first and then use that as an input to create my PDF, but ends up with the same error as above. The code sample below

            _logger.LogInformation($"HTML file created filename - {filePath}");

            // Load the HTML file and convert to PDF.
            HtmlLoadOptions options = new HtmlLoadOptions();
            options.PageInfo.IsLandscape = orientation == "landscape";
            options.PageInfo.Margin = new MarginInfo(0, 0, 0, 0);

            Document pdfDocument = new Document(filePath, options);

            var outputStream = new MemoryStream();
            outputStream.Seek(0, SeekOrigin.Begin);
            pdfDocument.Save(outputStream);

Could someone please let me know the root cause of the issue?

@SivaMurugan

Can you please share the sample HTML data for our reference? We will test the scenario in our environment and address it accordingly. Also, please share the complete stack trace information as well.

Please find below the HTML that we are currently using to convert as PDF. Please note that this issue is reproducible only from the deployed environment. The localhost works fine without any problem.
Also I didn’t get any problem when I try to use the same HTML in your online PDF converter.

            <html>
            <head>
            <style>html {
                font-family: Lato, Arial, helvetica, sans-serif !important;
            }

            #student-certificate {
                position: absolute;
                display: flex;
                justify-content: center;
                align-items: center;
                left: 0;
                right: 0;
                margin: 25px;
                overflow: visible;
            }

            table {
                width: 100%;
                background-color: #fff;
                border-spacing: 0;
            }

            #cert-table { 
                border: 1px solid #828485;
                border-bottom-left-radius: 4px;
                border-bottom-right-radius: 4px;
            }

            #cert-table caption {
                display: table-caption;
                padding: 15px 14px 13px 14px;
                font-family: Lato, Helvetica, Arial, sans-serif;
                font-size: 18px;
                font-weight: bold;
                text-align: left;
                background-color: #eee;
                border: 1px solid #828485;
                border-bottom: 1px solid #b1b3b4;
                border-top-left-radius: 4px;
                border-top-right-radius: 4px;
            }
            
            #cert-table .label {
                font-weight: bold;
                white-space: nowrap;
                border-right: 1px solid #bcbec0;
            }
        
            #cert-table .citationtitle {
                padding: 16px 14px;
                font-weight: 700;
            }
        
            #cert-table .number {
                text-align: center;
            }
        
            #student-table {
                margin: 0;
                background-color: #fff;
                border-top: 0;
            }

            #student-table th,
            #student-table td {
                padding: 17px 14px;
                line-height: 25px;
                text-align: left;
                border-top: 1px solid #828485;
            }

            .no-top-border {
                border-top: none !important;
            }
        
            #student-table th {
                font-weight: bold;
                background-color: #eee;
            }
        
            #student-table td &.number {
                text-align: center;
            }
            
            #student-table .number {
                margin-right: 15px;
            }
        
            #summary-report th,
            #summary-report td {
                padding: 5px;
                text-align: center;
            }
        
            #summary-report th {
                font-weight: bold;
                border-top: 1px solid #828485;
                border-bottom: 1px solid #828485;
                background-color: #eee;
            }

            #answer-report th {
                padding: 5px;
                background-color: #eee;
                border-top: 1px solid #828485;
                border-bottom: 1px solid #828485;
            }
        
            #answer-report td {
                padding: 15px 10px;
                vertical-align: top;
                border-bottom: 1px solid #b1b3b4                
            }
            
            .problem-number {
                padding-top: 22px !important;
            }

            .status .status-icon {
                position: absolute;
                height: 24px;
                width: 24px;
                margin: 3px 0 0 7px;
            }

            .attempt {
                display: flex;
                flex-direction: row;
                line-height: 25px;
            }

            .it,
            .italic {
                font-style: italic;
            }

            .smallcaps {
                font-variant: small-caps;
                font-size: 12px;
            }

            .bf {
                font-weight: bold;
            }

            .error {
                position: relative;
                color: #222;
                background-color: #e5b5c9;
            }</style></head>
            <article id='student-certificate'>
              <div id='certificate'>
                <table id='cert-table'>
                  <caption>Certificate of Completion:&nbsp;&nbsp;&nbsp;Grades - 1:&nbsp;&nbsp;Case Names</caption>
                  <tbody>
                    <tr>
                      <td>
                        <table id='student-table'>
                          <tbody>
                            <tr>
                              <td class='label no-top-border'>Student Name</td>
                              <td class='no-top-border'></td>
                            </tr>
                            <tr>
                              <td class='label'>Completion Date &amp; Time</td>
                              <td>Jul 26, 2023 03:20 PM</td>
                            </tr>
                            <tr>
                              <td class='label'>Primary Professor</td>
                              <td>
                              </td>
                            </tr>
                          </tbody>
                        </table>
                      </td>
                    </tr>
                    <tr>
                      <td>
                        <table id='summary-report'>
                          <tbody>
                            <tr>
                              <th class='number'>Total Problems</th>
                              <th class='number'>Correct</th>
                              <th class='number'>Incorrect</th>
                              <th class='number'>Total Attempts</th>
                              <th class='number'>Average Attempts per Problem</th>
                            </tr>
                            <tr>
                              <td class='number'>15</td>
                              <td class='number'>0</td>
                              <td class='number'>1</td>
                              <td class='number'>1</td>
                              <td class='number'>1</td>
                            </tr>
                          </tbody>
                        </table>
                      </td>
                    </tr>
                    <tr>
                      <td>
                        <table id='answer-report'>
                          <tbody>
                            <tr>
                              <th>Problem</th>
                              <th>Status</th>
                              <th>Correct Answer</th>
                              <th>Attempts</th>
                            </tr>	
                            <tr>
                              <td class='problem-number number'>1</td>
                              <td class='status'>
                                <img src='C:\dev\FeaturePeer.ICW\server\src\FeaturePeer\images\icw-exercise-incorrect.png class='status-icon' />
                              </td>
                              <td class='correct-answer'> <span class = "italic">People v.</span> <span class = "italic">Vasquez</span></td>
                              <td class='attempts'>
                                <p class='attempt'>
                                    <span class='number'>1.</span>
                                    <span><span><span class="error">A single action in the Appellate Division &amp; &amp; &amp; of the New York Supreme &amp; Court, styled The People of New York v.</span> <span class="error">Vasquez</span><span class="error">, The &amp; People of New York v. Dalton, and The People of New York v. Adkinson.</span></span></span>
                                </p>
                              </td>
                            </tr>	
                            <tr>
                              <td class='problem-number number'>2</td>
                              <td class='status'>
                              </td>
                              <td class='correct-answer'></td>
                              <td class='attempts'>
                              </td>
                            </tr>	
                            <tr>
                              <td class='problem-number number'>3</td>
                              <td class='status'>
                              </td>
                              <td class='correct-answer'></td>
                              <td class='attempts'>
                              </td>
                            </tr>	
                            <tr>
                              <td class='problem-number number'>4</td>
                              <td class='status'>
                              </td>
                              <td class='correct-answer'></td>
                              <td class='attempts'>
                              </td>
                            </tr>	
                            <tr>
                              <td class='problem-number number'>5</td>
                              <td class='status'>
                              </td>
                              <td class='correct-answer'></td>
                              <td class='attempts'>
                              </td>
                            </tr>	
                            <tr>
                              <td class='problem-number number'>6</td>
                              <td class='status'>
                              </td>
                              <td class='correct-answer'></td>
                              <td class='attempts'>
                              </td>
                            </tr>	
                            <tr>
                              <td class='problem-number number'>7</td>
                              <td class='status'>
                              </td>
                              <td class='correct-answer'></td>
                              <td class='attempts'>
                              </td>
                            </tr>	
                            <tr>
                              <td class='problem-number number'>8</td>
                              <td class='status'>
                              </td>
                              <td class='correct-answer'></td>
                              <td class='attempts'>
                              </td>
                            </tr>	
                            <tr>
                              <td class='problem-number number'>9</td>
                              <td class='status'>
                              </td>
                              <td class='correct-answer'></td>
                              <td class='attempts'>
                              </td>
                            </tr>	
                            <tr>
                              <td class='problem-number number'>10</td>
                              <td class='status'>
                              </td>
                              <td class='correct-answer'></td>
                              <td class='attempts'>
                              </td>
                            </tr>	
                            <tr>
                              <td class='problem-number number'>11</td>
                              <td class='status'>
                              </td>
                              <td class='correct-answer'></td>
                              <td class='attempts'>
                              </td>
                            </tr>	
                            <tr>
                              <td class='problem-number number'>12</td>
                              <td class='status'>
                              </td>
                              <td class='correct-answer'></td>
                              <td class='attempts'>
                              </td>
                            </tr>	
                            <tr>
                              <td class='problem-number number'>13</td>
                              <td class='status'>
                              </td>
                              <td class='correct-answer'></td>
                              <td class='attempts'>
                              </td>
                            </tr>	
                            <tr>
                              <td class='problem-number number'>14</td>
                              <td class='status'>
                              </td>
                              <td class='correct-answer'></td>
                              <td class='attempts'>
                              </td>
                            </tr>	
                            <tr>
                              <td class='problem-number number'>15</td>
                              <td class='status'>
                              </td>
                              <td class='correct-answer'></td>
                              <td class='attempts'>
                              </td>
                            </tr>
                          </tbody>
                        </table>
                      </td>
                    </tr>
                  </tbody>
                </table>
              </div>
            </article>
          </html

Stack Trace

at #=za75$VPuq5MArDDRe7f4NFxxFCce1XIDVdg==.#=z9au5rCGRqL4f()
at #=za75$VPuq5MArDDRe7f4NFxxFCce1XIDVdg==…ctor(Stream #=zkfE32z0=, String #=z$oF49Ig=, Boolean #=z$nd0OSS0_h6K)
at #=za75$VPuq5MArDDRe7f4NFxxFCce1XIDVdg==…ctor(String #=zt3OX6q0=)
at Aspose.Pdf.Document.#=z9u7c6y8=(String #=zt3OX6q0=, String #=z$oF49Ig=)
at Aspose.Pdf.Document…ctor(String filename)

@SivaMurugan

Instead of Aspose.PDF for .NET, can you please install Aspose.PDF.Drawing from NuGet in your project and try again. In case issue keeps occurring, please let us know. We will log a ticket in our issue tracking system and share the ID with you.

@asad.ali . Thanks for the response. I tried with the Aspose.PDF.Drawing package, but still not able to get the PDF generated. Here is the error details.

FYI, I am using the HTML file load option here instead of using the HTML string to generate the PDF

  at #=znlEvUxzsdZ95jGjqpBUJB7AJgHBG4HngTK1BTrA=.#=zsuXy3av9$OD_(#=z6jWF9ElryXArxxSr9cVW8PPSdAkXmCt1Xw== #=z0p1XIqo=, Char #=zSNJFsUo=)         at #=znlEvUxzsdZ95jGjqpBUJB7AJgHBG4HngTK1BTrA=.#=zsuXy3av9$OD_(#=z6jWF9ElryXArxxSr9cVW8PPSdAkXmCt1Xw== #=z0p1XIqo=)         at #=zTcmheW2kLJ2bX9JhY0tS17mA2VmqH5PyBA==.#=zre1H1oBNVGH0(#=zTcmheW2kLJ2bX9JhY0tS17mA2VmqH5PyBA== #=zdvZi9ks=, #=zYKcOg2Q3xi3JuIxBvlRnusI442UyyxGIrCuZ1n4= #=zUKLw3S0=, #=zrJ1k69oIrv8xJMmMblu1uyOFP0Rb1Smjkg== #=zr2drt8I=)         at #=zQ7Q8eeBLfKm4pIi$snx961KOkfzMQXp4_Zq8kis=..ctor(#=zTcmheW2kLJ2bX9JhY0tS17mA2VmqH5PyBA== #=zdvZi9ks=, #=zkizDnWC8chiJoEpohi2EhkcPFxD4 #=zncHgIAs=, #=zYKcOg2Q3xi3JuIxBvlRnusI442UyyxGIrCuZ1n4= #=zUKLw3S0=)         at #=zTcmheW2kLJ2bX9JhY0tS17mA2VmqH5PyBA==..ctor(#=zk1qssx3d51bPEvHniBteQlk= #=zvZkFd3U=, #=zYKcOg2Q3xi3JuIxBvlRnusI442UyyxGIrCuZ1n4= #=zUKLw3S0=, #=zTcmheW2kLJ2bX9JhY0tS17mA2VmqH5PyBA== #=zweYtH4rSK2XN)         at #=zTcmheW2kLJ2bX9JhY0tS17mA2VmqH5PyBA==..ctor(#=z0qSfyA1HudPpsglbtvviMws= #=zWCWRNaM=, #=zTcmheW2kLJ2bX9JhY0tS17mA2VmqH5PyBA== #=zweYtH4rSK2XN)         at #=z9LDR0$ahXcOp90XLBBrrJPf4BHv1qeCz2w==.#=zWlHr2KB38CPQ(#=z0qSfyA1HudPpsglbtvviMws= #=zWCWRNaM=, #=zDw4pDqY6K1HxxsBhgcbJpD0HGbXyUvR7hQ== #=zpo5ORUM=, #=zllZG4IaWmX8jDBPIOa_ENC_LNEA_ar9QUw== #=z0Gi4OzCAFe2M, #=zG2AGZnB97DoMQUgFWkzwZFXW5RVe #=zJcZkiKg=, #=zTcmheW2kLJ2bX9JhY0tS17mA2VmqH5PyBA== #=zweYtH4rSK2XN, #=zxqFBNF8Zl3cZbqsJQuNp1HHW2U7wkY9vXg== #=zsUpkXuDbLNppcfaByQ==)         at #=z9LDR0$ahXcOp90XLBBrrJPf4BHv1qeCz2w==.#=zjURZAow=(#=z0qSfyA1HudPpsglbtvviMws= #=zWCWRNaM=, #=zDw4pDqY6K1HxxsBhgcbJpD0HGbXyUvR7hQ== #=zpo5ORUM=, #=zllZG4IaWmX8jDBPIOa_ENC_LNEA_ar9QUw== #=z0Gi4OzCAFe2M, #=zxqFBNF8Zl3cZbqsJQuNp1HHW2U7wkY9vXg== #=zsUpkXuDbLNppcfaByQ==)         at #=zlL0yPOonjmQQdnjseL07YTzm_Ycpkynz4g==.#=zJwjHxwhbGmBS(#=zcYOwa2DKi9ikHI_NV$QWHg4= #=zCo7XP8s=, #=zuSsyhxhe9NzYlSO7nclNsZNU0czS #=zESWtFUXBW1uAIkHqRA==, #=z760mL$tXf1wE7e6SrZbXFRHR_iZe #=zFMMqLmI=, #=zSZGgSqZ8UC4pNFIXRTAFarf_nfsH #=zD8GhBsQ=, #=zmBwpalQNVdcyUlkmFZR62eSKYppX0rpaShkxj14= #=zaIGgVrG113l$4H_NtKZWX3U=, #=zZtqAzil4x3cIZnhZ85t6Gdk= #=z$iTiqzNMbIzIs40H2g==)         at #=zlL0yPOonjmQQdnjseL07YTzm_Ycpkynz4g==.#=ztOfq64Q=(#=z760mL$tXf1wE7e6SrZbXFRHR_iZe #=zFMMqLmI=, #=zSZGgSqZ8UC4pNFIXRTAFarf_nfsH #=zD8GhBsQ=)         at #=zSZGgSqZ8UC4pNFIXRTAFarf_nfsH.#=ztOfq64Q=(#=z760mL$tXf1wE7e6SrZbXFRHR_iZe #=zFMMqLmI=)         at #=zUNANKYRYiXODrT_f5p8vGpncCgx2yzFIvA==.#=zWpvm6Gg=()         at #=zUNANKYRYiXODrT_f5p8vGpncCgx2yzFIvA==.#=zbOf9gt8=()         at #=zlGzMeNNjDufUGzltlBhQp4wvnyboVvf6PQ==.#=z1E$Sg29TOfML(#=zx95q$KeCPqy3pzg_7HIbbAg= #=z_7$Fuvs_Rf_cyY1H9Q==, #=zSZGgSqZ8UC4pNFIXRTAFarf_nfsH[] #=z8237WSzzBfv_, #=z760mL$tXf1wE7e6SrZbXFRHR_iZe #=zFMMqLmI=, CancellationToken #=zS5D6$$M=)         at #=zCuETAWWVHwf8kYlsKKx7zulCAckO.Render(#=z760mL$tXf1wE7e6SrZbXFRHR_iZe #=zFMMqLmI=, CancellationToken #=zS5D6$$M=, #=z0qSfyA1HudPpsglbtvviMws=[] #=zafyyJQ4=, #=zcYOwa2DKi9ikHI_NV$QWHg4=[] #=zxF16xzw=)         at #=zCuETAWWVHwf8kYlsKKx7zulCAckO.Render(#=z760mL$tXf1wE7e6SrZbXFRHR_iZe #=zFMMqLmI=, TimeSpan #=zrLkaIsY=, #=zcYOwa2DKi9ikHI_NV$QWHg4=[] #=zxF16xzw=)         at #=zphza2laDM5FdyuXJutMfPyYvQwV1.Render(#=z760mL$tXf1wE7e6SrZbXFRHR_iZe #=zFMMqLmI=, #=z_paH9b0= #=zCo7XP8s=, TimeSpan #=zrLkaIsY=)         at #=zCQRcefdqjS9xXXQX$B1Z3VWaaACx.#=z$GVWHqGhxkKD(Stream #=zF5D1cMJTvHml, Document #=zCoyL0DfYnSmT, HtmlLoadOptions #=zTJaKW5uAQ9uj, String #=z9T1K$Zi08Uom)         at #=zCQRcefdqjS9xXXQX$B1Z3VWaaACx.#=z3Q7zvR0=(Stream #=zF5D1cMJTvHml, Document #=zCoyL0DfYnSmT, HtmlLoadOptions #=zTJaKW5uAQ9uj, String #=z9T1K$Zi08Uom)         at #=zCQRcefdqjS9xXXQX$B1Z3VWaaACx.#=z3Q7zvR0=(Stream #=zF5D1cMJTvHml, Document #=zCoyL0DfYnSmT, HtmlLoadOptions #=zTJaKW5uAQ9uj)         at Aspose.Pdf.Document.#=ziu3IFPM=(Stream #=zh$Ez3JE=, LoadOptions #=zLOZHe1U=)

@SivaMurugan

We need to investigate this case in details. Therefore, we have opened the following new ticket(s) in our internal issue tracking system and will deliver their fixes according to the terms mentioned in Free Support Policies.

Issue ID(s): PDFNET-55194

You can obtain Paid Support Services if you need support on a priority basis, along with the direct access to our Paid Support management team.