Spire.PDF is a professional PDF library applied to creating, writing, editing, handling and reading PDF files without any external dependencies. Get free and professional technical support for Spire.PDF for .NET, Java, Android, C++, Python.

Wed Jul 22, 2020 4:39 pm

Hi

Currently we are generating dynamic html to pdf in C# but external css(demo.css) are not applying to the generated pdf
Could you please help on below code

below are the sample code we are using Spire.Pdf(5.6.3.18046 version) and Spire.Doc(7.6.2.18046 version).

using Spire.Doc;
using Spire.Pdf;
using Spire.Pdf.HtmlConverter;

public bool PDFConvert()
{
string htmlCode = "<html><head><link href="demo.css" /></head><body>"<div class="centersection row margin-right-20"><p class="title-bold font-20 color-red"> this is a sample dynmic html to pdf </p></div>"</body></html>";

PdfDocument pdf = new PdfDocument();
PdfHtmlLayoutFormat htmlLayoutFormat = new PdfHtmlLayoutFormat();
htmlLayoutFormat.IsWaiting = false;
PdfPageSettings setting = new PdfPageSettings();
setting.Size = PdfPageSize.A4;

byte[] buffer;

Thread thread = new Thread(() =>
{
pdf.LoadFromHTML(htmlCode, true, setting, htmlLayoutFormat);

using (MemoryStream ms = new MemoryStream())
{
/*Default File for Preview of PDF */
using (FileStream to_stream = new FileStream("sampledemo.pdf", FileMode.Create, FileAccess.Write))
{
pdf.SaveToStream(ms, Spire.Pdf.FileFormat.PDF);
ms.WriteTo(to_stream);
buffer = ms.ToArray();
to_stream.Close();
ms.Close();
System.IO.File.WriteAllBytes(@"C:\SampleDemo\sampledemo.pdf", buffer);


}
}

});
thread.SetApartmentState(ApartmentState.STA);
thread.Start();
thread.Join();
return true;
}

sample file FYI... DEMO.css


.centersection
{
align:center;
}

.margin-right-20
{
margin-right:20px;
}

.title-bold
{
font-type:bold;
}
. font-20{
font-size:20px;
}

.color-red
{
color:red;
}

AbdulRehman
 
Posts: 1
Joined: Wed Jul 22, 2020 3:59 pm

Thu Jul 23, 2020 8:04 am

Hello,

Thanks for your post.
When viewing your css settings in a browser, I found that only the color setting works. Please refer to the attachment to modify the css file and use our latest version (Spire.PDF (hot fix) version 6.7.8) to try again. In addition, our Spire.PDF supports converting HTML to PDF with plugin. Generally, this way will get a better output. You can refer to the tutorial (Convert HTML to PDF with New Plugin) to test. If there is any question, just feel free to write back.

Sincerely,
Sofia
E-iceblue support team
User avatar

Sofia.Yang
 
Posts: 84
Joined: Tue Jul 14, 2020 1:41 am

Fri Jul 31, 2020 10:55 am

Hi Rehman,

Greetings from E-iceblue.
Regarding the issue you posted on our forum, did we resolve your issue?
Thanks in advance for your valuable feedback and time.

Sincerely,
Sofia
E-iceblue support team
User avatar

Sofia.Yang
 
Posts: 84
Joined: Tue Jul 14, 2020 1:41 am

Return to Spire.PDF