Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to add css to pdf #356

Closed
onopriienkoalina opened this issue Nov 16, 2023 · 3 comments
Closed

How to add css to pdf #356

onopriienkoalina opened this issue Nov 16, 2023 · 3 comments

Comments

@onopriienkoalina
Copy link

Please check if it satisfies the following requirements:

  • It is a small project satisfying a particular scenario
  • It is related to Aspose APIs
  • It takes 8 or less hours for us to complete
  • It is not a bug
  • It is not a feature request

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
Copy link

@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
Copy link
Author

onopriienkoalina commented Nov 18, 2023 via email

@asadalikhan90
Copy link

@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?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants