Issue generating PDF using html string
See original GitHub issueI use the following code to generate a PDF using the ASPOSE.PDF package. My requirements meant that while generating the PDF, I need the output in a memory stream and not saved to disk. So couldn’t use the aspose code that takes a save file path as a parameter. Here’s what I had:
public Stream GeneratePdfStream(string html)
{
// Verify our license is loaded before using.
VerifyLicense();
// Turn our string into bytes using encoding.
using var stream = new MemoryStream(Encoding.ASCII.GetBytes(html ?? string.Empty));
// Create HTML load options
HtmlLoadOptions htmloptions = new HtmlLoadOptions();
htmloptions.HtmlMediaType = HtmlMediaType.Print;
htmloptions.PageInfo.Margin.Bottom = 0;
htmloptions.PageInfo.Margin.Top = 0;
htmloptions.PageInfo.Margin.Right = 0;
htmloptions.PageInfo.Margin.Left = 0;
// Load HTML file
Document pdfDocument = new Document(stream, htmloptions);
// Convert HTML file to PDF
Stream memoryStream = new MemoryStream();
// Save output as PDF format
pdfDocument.Save(memoryStream);
memoryStream.Seek(0, SeekOrigin.Begin);
return memoryStream;
}
This means that I can then download the stream for the PDF via a web API or I can convert to Base64, depending on my use case. Calling this method with this parameter works fine:
var html = "<html><body><h1>This is a test</h1></body></html>";
var pdfStream = GeneratePdfStream(html);
using (FileStream fs = File.Create("somePath.pdf"))
{
fs.CopyTo(pdfStream);
}
However, when I use a more real-world example, i.e. this:
// Roughly, a 10kb worth of content here.
var html = "<!doctype html> <html lang='en'> <head> <meta charset='utf-8'> <meta charset='utf-8'> <meta http-equiv='X-UA-Compatible' content='IE=edge'> <title>Invoice</title> <style> html { font-family: sans-serif; -ms-text-size-adjust: 100%; -webkit-text-size-adjust: 100%; } body { margin: 0; } strong { font-weight: 700; } h1 { font-size: 2em; margin: 0.67em 0; } sup { font-size: 75%; line-height: 0; position: relative; vertical-align: baseline; top: -0.5em; } img { border: 0; } table { border-collapse: collapse; border-spacing: 0; } td, th { padding: 0; } html { font-size: 10px; -webkit-tap-highlight-color: #fff; } body { font-family: 'Helvetica Neue', Helvetica, Arial, sans-serif; font-size: 14px; line-height: 1.42857143; color: #333; background-color: #fff; } img { vertical-align: middle; } h1, h2, h3 { font-family: inherit; font-weight: 500; line-height: 1.1; color: inherit; } h1, h2, h3 { margin-top: 20px; margin-bottom: 10px; } h1 { font-size: 36px; } h2 { font-size: 30px; } h3 { font-size: 24px; } p { margin: 0 0 5px; } .text-right { text-align: right; } .row { margin-right: -15px; margin-left: -15px; } .col-xs-2, .col-xs-4, .col-xs-8, .col-xs-10, .col-xs-12 { position: relative; min-height: 1px; padding-right: 15px; padding-left: 15px; float: left; } .col-xs-12 { width: 100%; } .col-xs-10 { width: 83.33333333%; } .col-xs-8 { width: 66.66666667%; } .col-xs-4 { width: 33.33333333%; } .col-xs-2 { width: 16.66666667%; } table { background-color: transparent; } th { text-align: left; } .table { width: 100%; max-width: 100%; margin-bottom: 20px; } .table > thead > tr > th, .table > tbody > tr > th, .table > tfoot > tr > th, .table > thead > tr > td, .table > tbody > tr > td, .table > tfoot > tr > td { padding: 8px; line-height: 1.42857143; vertical-align: top; border-top: 1px solid #ddd; } .table > thead > tr > th { vertical-align: bottom; border-bottom: 2px solid #ddd; } .table > caption + thead > tr:first-child > th, .table > colgroup + thead > tr:first-child > th, .table > thead:first-child > tr:first-child > th, .table > caption + thead > tr:first-child > td, .table > colgroup + thead > tr:first-child > td, .table > thead:first-child > tr:first-child > td { border-top: 0; } .table > tbody + tbody { border-top: 2px solid #ddd; } .row:before, .row:after { display: table; content: ' '; } .row:after { clear: both; } body { width: 100%; height: 100%; margin: 0; padding: 0; font-family: 'Helvetica Neue', Helvetica, Arial, 'Lucida Grande', sans-serif; font-weight: 400; color: #000; font-size: 8pt; line-height: 1.5; } * { box-sizing: border-box; -moz-box-sizing: border-box; } .page { width: 210mm; min-height: 297mm; padding: 10mm 20mm; margin: 10mm auto; position: relative; background: #fff; box-shadow: 0 0 5px #d2d2d2; } h1 { font-size: 18pt; font-weight: 500; margin: 30px 0 20px 0; } h2 { font-size: 12pt; font-weight: 700; text-transform: uppercase; } h3 { font-size: 8pt; font-weight: 700; text-transform: uppercase; margin: 0 0 10px 0; } .logo { position: absolute; left: 20mm; width: 50mm; height: 20mm; } .addresses { display: -webkit-box; display: -moz-box; display: -webkit-flex; display: -ms-flexbox; display: flex; } .addresses .address-col { line-height: 1.5; padding: 0; min-width: 28%; max-width: 35%; display: inline-block; -ms-flex: 1 1 auto; flex: 1 1 auto; } .addresses .address-divider { border-left: 1px solid #b0b0b0; display: block; padding: 0 4mm; } .date-info { text-align: right; } .date-info p { font-size: 9pt; line-height: 1.2; margin: 0 0 0.9rem 0; } .registration-footer { border-top: 1px solid #b0b0b0; display: block; width: 100%; padding: 15px 15px 0 15px; } .reason-count { margin: 0 5px; } .table-invoice { margin-top: 10px; border-bottom: 1px solid #000; } .table-invoice thead tr th { -webkit-print-color-adjust: exact !important; background-color: #000; color: #fff; border-bottom: 0 none !important; vertical-align: middle !important; padding: 10px 15px; } .table-invoice thead tr th:nth-child(6) { width: 12%; padding: 0; padding-right: 10px; } .table-invoice div.incl-vat-text { font-size: 10px; } .table-invoice tbody tr td { padding: 5px 15px; border-color: #b0b0b0; } .table-invoice tbody tr:first-of-type td { border-top: 0 none; } .table-invoice.side-borders { border-bottom: 0 none; } .table-invoice.side-borders thead tr th { border: 1px solid #ccc; } .table-invoice.side-borders tbody tr td { border: 1px solid #ccc; } .table-totals tbody tr td { padding: 0 0 10px 0; border: 0 none; } .text-watermark { -webkit-print-color-adjust: exact !important; position: absolute; top: 40%; left: 0; width: 210mm; text-align: center; font-size: 110pt; color: #e6e6e6 !important; -ms-transform: rotate(-48deg); -webkit-transform: rotate(-48deg); transform: rotate(-48deg); } @@media print { html, body { width: 210mm; height: auto; } .page { margin: 0; border: initial; border-radius: initial; width: initial; min-height: initial; box-shadow: initial; background: initial; page-break-after: always; } .table-invoice { border-bottom: 1px solid #000; } .table-invoice thead tr th { -webkit-print-color-adjust: exact !important; background-color: #000 !important; color: #fff !important; } .text-watermark { -webkit-print-color-adjust: exact !important; } } .pr-0, .px-0 { padding-right: 0 !important; } .pl-0, .px-0 { padding-left: 0 !important; } .text-right { text-align: right !important; } .m-0 { margin: 0 !important; } .mb-0, .my-0 { margin-bottom: 0 !important; } .mb-1, .my-1 { margin-bottom: 0.25rem !important; } .mb-2, .my-2 { margin-bottom: 0.5rem !important; } .mt-3, .my-3 { margin-top: 1rem !important; } .mb-3, .my-3 { margin-bottom: 1rem !important; } </style> </head> <body> <div id='invoice' class='page'> <img src='https://svgur.com/i/NRB.svg' class='logo' alt='examples' /> <div class='row px-0'> <div class='col-xs-12 text-right text-light'> example's<sup>®</sup> Customer Services<br /> P: 00800 53847 501<br /> https://www.example.com/GB/en_GB/contact<br /> Shop: example.com </div> </div> <div class='row px-0'> <div class='col-xs-12 text-right'> <h1>Invoice</h1> </div> </div> <div class='row px-0'> <div class='col-xs-10'> <div class='addresses'> <div class='address-col'> <h3><strong>BILL TO</strong></h3> <p> {{BillingAddress:FirstName}} {{BillingAddress:LastName}}<br /> {{BillingAddress:Address1}}<br /> {{BillingAddress:Address2}}<br /> {{BillingAddress:Address3}}<br /> {{BillingAddress:City}}<br /> {{BillingAddress:PostalCode}}<br /> {{BillingAddress:Country}} </p> <p class='mb-0'><strong>P:</strong> {{BillingAddress:Telephone}}</p> </div> <div class='address-divider'></div> <div class='address-col'> <h3><strong>SHIP TO</strong></h3> <p> {{ShippingAddress:FirstName}} {{ShippingAddress:LastName}}<br /> {{ShippingAddress:Address1}}<br /> {{ShippingAddress:Address2}}<br /> {{ShippingAddress:Address3}}<br /> {{ShippingAddress:City}}<br /> {{ShippingAddress:PostalCode}}<br /> {{ShippingAddress:Country}} </p> <p class='mb-0'><strong>P:</strong> {{ShippingAddress:Telephone}}</p> </div> <div class='address-divider'></div> <div class='address-col'> <h3><strong>SOLD BY</strong></h3> <p> example Strauss & Co Europe<br /> Airport Building<br /> someplace 19<br /> 1831 Diegem<br /> Belguim </p> </div> </div> </div> <div class='col-xs-2 date-info'> <p> <strong>Order No.</strong><br /> {{ORDERNUMBER}} </p> <p> <strong>Order Date</strong><br /> {{ORDERDATE}} </p> <p> <strong>Invoice No.</strong><br /> {{INVOICENUMBER}} </p> <p> <strong>Issue Date</strong><br /> {{ORDERDATE}} </p> </div> </div> <div class='row px-0'> <div class='col-xs-10'> <p class='mt-3'><strong>Dear Customer,</strong></p> <p>Thank you for shopping on example.com. </p> <p>If your order comes in multiple shipments, each invoice will show the initial shipping charge but you will only be charged once for your order.</p> <p>If you have any questions relating to your purchase, please visit us at example.com. Please check our Help Centre section for further guidance.</p> <p>In case of returns, please note that you have 28 days to return any item.</p> <p>We look forward to seeing you next time.</p> <p class='mb-0'>example's<sup>®</sup> Customer Service</p> </div> </div> <div class='row px-0'> <div class='col-xs-12 px-0'> <p class='text-right'><strong>VAT amount and price are in: GBP</strong></p> <table class='table table-invoice'> <thead> <tr> <th>Product Code</th> <th>Description</th> <th>Size</th> <th>Qty</th> <th>VAT%</th> <th class='text-right'> Line Total <div class='incl-vat-text'>(Inc VAT)</div> </th> </tr> </thead> <tbody> <tr> <td>ABC123</td> <td> Umbrella1 </td> <td>100cm x 100cm</td> <td>1</td> <td class='text-right'>19.99</td> <td class='text-right'>60.00</td> </tr> <tr> <td>ZXY987</td> <td> Umbrella2 </td> <td>100cm x 100cm</td> <td>1</td> <td class='text-right'>19.99</td> <td class='text-right'>60.00</td> </tr> </tbody> </table> </div> </div> <div class='row px-0'> <div class='col-xs-8'> <h3 class='mb-3'>Payment Info</h3> <p class='mb-1'><strong>Customer No.</strong> {{CUSTOMERNUMBER}}</p> <p class='mb-1'><strong>Payment Method.</strong> {{PAYMENTMETHOD}}</p> </div> <div class='col-xs-4'> <table class='table table-totals'> <tbody> <tr> <td><h3 class='m-0'>SUBTOTAL</h3></td> <td class='text-right'>{{CURRENCYSYMBOL}}{{SUBTOTAL}}</td> </tr> <tr> <td><strong>SHIPPING</strong></td> <td class='text-right'>{{CURRENCYSYMBOL}}{{SHIPPINGTOTAL}}</td> </tr> <tr> <td><strong>VAT</strong></td> <td class='text-right'>{{CURRENCYSYMBOL}}{{VAT}}</td> </tr> <tr> <td><h2 class='m-0'>TOTAL</h2></td> <td class='text-right'><h2 class='m-0'>{{CURRENCYSYMBOL}} {{TOTAL}}</h2></td> </tr> </tbody> </table> </div> </div> <div class='row px-0'> <div class='col-xs-12 px-0'> <div class='registration-footer'> <p class='mb-2'> <strong>Vat Registration Number:</strong> GB 111 111 111 </p> <p class='mb-2'><strong>Company Registration Number:</strong> 0424 656 991</p> <p class='mb-2'><strong>Place Of Registration:</strong> 181 someplace, Canada</p> <p class='mb-2'><strong>Registered office address:</strong> Someplace 19, 181 Someotherplace</p> </div> </div> </div> </div> </body> </html>";
var pdfStream = GeneratePdfStream(html);
using (FileStream fs = File.Create("somePath.pdf"))
{
fs.CopyTo(pdfStream);
}
This fails at the following line in the GeneratePdf method:
Document pdfDocument = new Document(stream, htmloptions);
It never steps beyond this line. I can see this output to debug console in Visual Studio:
Exception thrown: 'System.MissingMethodException' in System.Private.CoreLib.dll
'testhost.exe' (CoreCLR: clrhost): Loaded 'zx_58deb2a6301a4e96b0b71407b2d74f51'. Module was built without symbols.
Exception thrown: '#=zT$tWUPWC0$36rF0x5R0MIrd44UNN_lb$xzK12V4=' in Aspose.Pdf.dll
Exception thrown: '#=zT$tWUPWC0$36rF0x5R0MIrd44UNN_lb$xzK12V4=' in Aspose.Pdf.dll
Exception thrown: '#=zT$tWUPWC0$36rF0x5R0MIrd44UNN_lb$xzK12V4=' in Aspose.Pdf.dll
Exception thrown: '#=zT$tWUPWC0$36rF0x5R0MIrd44UNN_lb$xzK12V4=' in Aspose.Pdf.dll
Exception thrown: 'System.InvalidOperationException' in Aspose.Pdf.dll
I only reference the Aspose.Pdf package, no others. What would be wrong with the approach above? I also tried the approach recommended here: https://docs.aspose.com/display/htmlnet/HTML+to+PDF+Conversion and also get the same issue with the html string above.
Thanks
Issue Analytics
- State:
- Created 3 years ago
- Comments:5 (3 by maintainers)
Top GitHub Comments
@rmccabe24
We have shared our forum link in our previous message. Here is it: https://forum.aspose.com/c/pdf
@rmccabe24
We have tested the case using 22.5 version of Aspose.PDF for .NET and could not notice the exception. Please try using the latest version of the API and let us know in case you face any issues. Also, please try to post your inquiry on official Aspose.PDF Support forum which is the right place to discuss such issues.