Adding SKU to the invoice .pdf

This topic was automatically closed 365 days after the last reply. New replies are no longer allowed.
11 years ago
Hi Tim,

Just wondering do you have any idea how to implement Exporting the catalog to Pdf from the frontend, I know it can be done in Admin but can users do it?? Or can it be done.

Kind regards

Richard
11 years ago
wertyuio1 wrote:
Hi Tim,

Just wondering do you have any idea how to implement Exporting the catalog to Pdf from the frontend, I know it can be done in Admin but can users do it?? Or can it be done.

Kind regards

Richard


I spent some time playing around with it but it looks like the URL to download the PDF catalog is denied if the user is not an administrator.

If you look in

ProductController.cs

Presentation -> Nop.Web -> Administration -> Controllers -> ProductController.cs

You can find the following code:

///////////////////////////////////////////////////////////////////

public ActionResult DownloadCatalogAsPdf()
        {
            if (!_permissionService.Authorize(StandardPermissionProvider.ManageCatalog))
                return AccessDeniedView();

            try
            {
                var products = _productService.SearchProducts(0, 0, null, null, null, 0, string.Empty, false,
                    _workContext.WorkingLanguage.Id, new List<int>(),
                    ProductSortingEnum.Position, 0, int.MaxValue, true);
                string fileName = string.Format("pdfcatalog_{0}_{1}.pdf", DateTime.Now.ToString("yyyy-MM-dd-HH-mm-ss"), CommonHelper.GenerateRandomDigitCode(4));
                string filePath = string.Format("{0}content\\files\\ExportImport\\{1}", this.Request.PhysicalApplicationPath, fileName);
                _pdfService.PrintProductsToPdf(products, _workContext.WorkingLanguage, filePath);
                var bytes = System.IO.File.ReadAllBytes(filePath);
                return File(bytes, "application/pdf", fileName);
            }
            catch (Exception exc)
            {
                ErrorNotification(exc);
                return RedirectToAction("List");
            }
        }  

//////////////////////////////////////////////////////////////////////

I was wondering this was the problem

if (!_permissionService.Authorize(StandardPermissionProvider.ManageCatalog))
                return AccessDeniedView();

I tried replacing the return AccessDeniedView(); with the code under the "try," changing the variable names, but I could not get it to work.  I also tried to take out the AccessDeniedView(); part but that did not do it.  However, you might want to look at this code and try playing with it.


Here is an easy way you could allow your customers to download a PDF of your catalog from the public store side.
  
Once you have added all your products to your store you could go to admin and download the PDF of your catalog to you computer.  Then you could upload your catalog to the root folder of your website and put a link to it on your store.

For example:

www.yourstore.com/catalog.PDF  

The good side of doing it this way would be that you would not have to worry that you had caused a security problem by allowing access to part of the admin side.

The bad side of doing it this way is that you would have to upload a new version of you catalog when you made any changes to your products.

I am sorry I don't know the best way to do this, but you could use this method as a quick fix.  I will let you know if I figure out the real way to do it.
11 years ago
Hi Tim,

I would use the work around you sugested but we have a 15minute live replication taking place.. So that would be great hassle.. :D

Yes I found the code I have inserted into Nop.Web.Controller.Catalog.cs as a method. In my headermenu I have a link but I'm trying to find the route but I can't seem to see it the Infrastructure route provider would you know where it would be by any chance.. I'm still digging through.

Thanks

Richard
11 years ago
I am not sure right now but I will take a look at it and see if I can figure it out.
11 years ago
I am two we can both work it out..

Thanks

R
11 years ago
I think you need to add a route in RouteProvider.cs

RouteProvider.cs

Presentation -> Nop.Web -> Infrastructure -> RouteProvider.cs

For example:

In CatalogController.cs

Presentation -> Nop.Web -> Controllers -> CatalogController.cs

It looks like the following code is for the recently added products

////////////////////////////////////////////////////////////////////////

        public ActionResult RecentlyAddedProducts()
        {
            var model = new List<ProductModel>();
            if (_catalogSettings.RecentlyAddedProductsEnabled)
            {
                var products = _productService.SearchProducts(0, 0, null, null,
                    null, 0, null, false, _workContext.WorkingLanguage.Id,
                    null, ProductSortingEnum.CreatedOn, 0, _catalogSettings.RecentlyAddedProductsNumber);
                foreach (var product in products)
                    model.Add(PrepareProductOverviewModel(product));
            }
            return View(model);
        }

////////////////////////////////////////////////////////////////////////



This is how it is referenced in RouteProvider.cs



////////////////////////////////////////////////////////////////////////

routes.MapLocalizedRoute("RecentlyAddedProducts",
                            "newproducts/",
                            new { controller = "Catalog", action = "RecentlyAddedProducts" },
                            new[] { "Nop.Web.Controllers" });

////////////////////////////////////////////////////////////////////////

Try adding a route for the code you have added using the same methodology.

P.S. Did you add the code to Catalog.cs or did you add it to CatalogController.cs?  I couldn't find Catalog.cs.

I hope this helps.
11 years ago
I added it to the CommonController.cs as I have set up a link in the header menu.

This what I have implemented but it doesn't seem to work and the runtime of the solution slo'w down dramatically in development.

I added the following

Nop.Web.Controller.CommonController.cs

Taken from the admin area.


         public ActionResult DownloadCatalogAsPdf()
        {
            var products = _productService.SearchProducts(0, 0, null, null, null, 0, string.Empty, false,
                _workContext.WorkingLanguage.Id, new List<int>(),
               ProductSortingEnum.Position, 0, int.MaxValue, true);
           string fileName = string.Format("pdfcatalog_{0}_{1}.pdf", DateTime.Now.ToString("yyyy-MM-dd-HH-mm-ss"),        CommonHelper.GenerateRandomDigitCode(4));
           string filePath = string.Format("{0}content\\files\\ExportImport\\{1}", this.Request.PhysicalApplicationPath, fileName);
           _pdfService.PrintProductsToPdf(products, _workContext.WorkingLanguage, filePath);
           var bytes = System.IO.File.ReadAllBytes(filePath);
            return File(bytes, "application/pdf", fileName);
        }


In  the route provider


routes.MapLocalizedRoute("DownloadCatalogAsPdf",
                            "DownloadCatalogAsPdf/",
                            new { controller = "Common", action = "DownloadCatalogAsPdf" },
                            new[] { "Nop.Web.Controllers" });


In the

headermenu.cshtl

<li><a href="@Url.Action("DownloadCatalogAsPdf")">@T("Admin.Catalog.Products.List.DownloadPDF")</a></li>


It didn't take just a really long loading time.

In the admin area there must be route for the button or am I overlooking something.
11 years ago
wertyuio1 wrote:
        

public ActionResult DownloadCatalogAsPdf()



Do you need to change the DownloadCatalogAsPdf() part to be different from the one on the admin side?


wertyuio1 wrote:

  In the admin area there must be route for the button or am I overlooking something.


If you go though a route on the admin side won't it rederect the customers to the login page?  That is what I ran into the other day when I tried to use the same URL as the one on the admin side.

Try looking at the code that is used to allow customers to download a PDF of their purchase invoice.  Customers can download this without being an administrator so following the same approach might do the trick.
11 years ago
Hi Tim

Finally had a chance to reslove DownloadCatalogPdf from the site.

I added a partial view to Customer directory with a public Action Result that return's the view. The view is routed in the Customer Navigation Block

Contained in the view is a Url Action.

The Action is based in the in the Customer Controller. I took the logic from the Administration controller and applyed it in the the new Action method.

On testing the CatalogPDF downloads fine.

If you want the code don't hesitate to ask.

Thank you

Richard...
This topic was automatically closed 365 days after the last reply. New replies are no longer allowed.