aspose-free-consulting / projects

The starting point for Aspose free consulting projects
5 stars 5 forks source link

How to add css to pdf #356

Closed onopriienkoalina closed 2 months ago

onopriienkoalina commented 6 months ago

Please check if it satisfies the following requirements:

If your requirement is actually a bug in our APIs or a feature request for our APIs then please use our forum to report it and get quick help: https://forum.aspose.com/

If all the above requirements are satisfied then please move on:

< describe your free consulting project requirements briefly> invoices project, i want to generate PDF from HTML. But pdf generets without css styles. Maybe I call string Html = RazorViewToStringFormat.RenderRazorViewToString(this, "IndexSummaryInvoices", summaryinvoices); in not correct place? Which place is correct to call for method RazorViewToStringFormat? < which platform you're working on -- operating system, development environment etc.> windows, asp.net mvc 5

namespace LandingPages.Common { public class RazorViewToStringFormat { /// /// Render razorview to string /// /// /// /// /// private SummaryInvoices mymodel = new SummaryInvoices(); public static string RenderRazorViewToString(Controller myControler, string viewName, object myModel) { myControler.ViewData.Model = myModel; var viewResult = ViewEngines.Engines.FindPartialView(myControler.ControllerContext, "IndexSummaryInvoices"); // checking the view inside the controller if (viewResult.View != null) { using (var sw = new StringWriter()) { var viewContext = new ViewContext(myControler.ControllerContext, viewResult.View, myControler.ViewData, myControler.TempData, sw); viewResult.View.Render(viewContext, sw); viewResult.ViewEngine.ReleaseView(myControler.ControllerContext, viewResult.View); return sw.GetStringBuilder().ToString(); } } else return "View cannot be found."; } public void GenerateAspose(Controller myControler) { try { string HtmlContent = RazorViewToStringFormat.RenderRazorViewToString(myControler, "IndexSummaryInvoices", mymodel); if (!HtmlContent.Equals("View cannot be found.")) { if (!HtmlContent.Equals("View cannot be found.")) //pdfAspose.GeneratePDF(HtmlContent); GeneratePDF(HtmlContent); //RazorViewToStringFormat razorView = new RazorViewToStringFormat(); // Instantiate the class //razorView.GeneratePDF(HtmlContent); // Call the method using the instantiated object } } catch (Exception ex) { // Handle exceptions } } public static void GeneratePDF(string HtmlContent) { Aspose.Pdf.License Objpdflicense = new Aspose.Pdf.License(); //Objpdflicense.SetLicense(@"E:\Aspose\Aspose.Pdf.lic"); //? Objpdflicense.Embedded = true; //Check if licensed applied //if (Document.IsLicensed) //{ //Set the properties for PDF file page format HtmlLoadOptions objLoadOptions = new HtmlLoadOptions(HttpContext.Current.Server.MapPath("~/Content/")); objLoadOptions.PageInfo.Margin.Bottom = 10; objLoadOptions.PageInfo.Margin.Top = 10; objLoadOptions.PageInfo.Margin.Left = 20; objLoadOptions.PageInfo.Margin.Right = 20; //Load HTML string into MemoryStream using Aspose document class Document doc = new Document(new MemoryStream(Encoding.UTF8.GetBytes(HtmlContent)), objLoadOptions); string FileName = "ProjectAudit_" + DateTime.Now.ToString("dd-MM-yyyy") + ".pdf"; //Save PDF file on local hard drive or database or as you wish doc.Save(HttpContext.Current.Server.MapPath("~/" + FileName)); System.Diagnostics.Process.Start(HttpContext.Current.Server.MapPath("~/" + FileName)); //} } } } Home controller: public ActionResult IndexSummaryInvoices(string jsonstr) { SummaryInvoicesIn myDeserializedObj = (SummaryInvoicesIn)JsonConvert.DeserializeObject((jsonstr), typeof(SummaryInvoicesIn)); SummaryInvoices summaryinvoices = new SummaryInvoices(); try { BusinessLogic bl = new BusinessLogic(); string strcon = bl.GetConData(myDeserializedObj.businessid); string strdb = WebConfigurationManager.AppSettings["DBStrStart"].ToString() + strcon + WebConfigurationManager.AppSettings["DBStrEnd"].ToString(); summaryinvoices = bl.GetSummaryInvoicesData(strdb, myDeserializedObj.typesearch, myDeserializedObj.fromdate, myDeserializedObj.todate, myDeserializedObj.customerid, myDeserializedObj.customername, myDeserializedObj.invoiceid); //// Clear the existing session variable for the database connection string this.Session["constr"] = null; //// Set the session variable to the new database connection string 'strdb' this.Session["constr"] = strdb; } catch (Exception e) { logger_.Error("IndexSummaryInvoices: " + e.Message); } if (!String.IsNullOrEmpty(summaryinvoices.businessrep.Logo)) ViewBag.Logo = summaryinvoices.businessrep.Logo; else ViewBag.Logo = "data:image/png; string Html = RazorViewToStringFormat.RenderRazorViewToString(this, "IndexSummaryInvoices", summaryinvoices); RazorViewToStringFormat.GeneratePDF(Html); return View(summaryinvoices); }
asadalikhan90 commented 6 months ago

@onopriienkoalina

If possible, can you please create an inquiry in our official support forum by sharing your sample HTML in .zip format? We will test the scenario in our environment and address it accordingly. Please note that our support forum is the right and appropriate place for such discussions.

onopriienkoalina commented 6 months ago

Thank you for your prompt response. I have taken the necessary steps to address the issue on your official support forum.

Please find the attached .zip file containing the necessary project files. I hope this assists your team in identifying and resolving the issue effectively.

Should you require any further information or have additional queries, feel free to let me know. I truly appreciate your support and attention to this matter.

Best regards,

Alina

сб, 18 лист. 2023 р. о 00:55 Asad Ali @.***> пише:

@onopriienkoalina https://github.com/onopriienkoalina

If possible, can you please create an inquiry in our official support forum https://forum.aspose.com/c/pdf by sharing your sample HTML in .zip format? We will test the scenario in our environment and address it accordingly. Please note that our support forum is the right and appropriate place for such discussions.

— Reply to this email directly, view it on GitHub https://github.com/aspose-free-consulting/projects/issues/356#issuecomment-1817223547, or unsubscribe https://github.com/notifications/unsubscribe-auth/AWHUSQ45SMUZ4YVGOPHECMLYE7TOVAVCNFSM6AAAAAA7OPOODWVHI2DSMVQWIX3LMV43OSLTON2WKQ3PNVWWK3TUHMYTQMJXGIZDGNJUG4 . You are receiving this because you were mentioned.Message ID: @.***>

asadalikhan90 commented 6 months ago

@onopriienkoalina

We could not find any .zip attached. Also, can you please share the link of the thread where you have reported this issue in the official support forum?