2

I have an input form page, that when the user submits, it updates that page with some tables of data. If the user likes what they see, they should be able to print a cleaned up (no header nav, footer nav, etc) version of those tables with formatting intact.

I'm creating the tables using bootstrap and angularjs. What I'm trying to do is that when the uset clicks 'print', my angular controller grabs the desired content from the generated page (in a div called 'template') and inserts it into a new html page that I can then open in a new window for printing.

I created a directive that I was hoping would allow me to use $compile to transclude my data:

app.directive('printTemplate', function () {
    return {
        templateUrl: 'app/partials/printtemplate.html',
        replace: true,
        transclude: true,
        restrict: 'A'
    };
});

And in my controller:

$scope.printTemplate = function () {
    var page = angular.element(template).contents();
    var newpage = $compile("<html><body><div print-template>" + page + "</div></body></html>")($scope);
    var win = window.open();
    win.document.write(newpage);
    //win.print();
};

But this doesn't seem to do what I expect. And it feels like I'm doing it wrong. The idea was that because my data tables require bootstrap script and css references I need to include those in my printtemplate.html file.

Is there an easier/better way to do this, or what am I missing?

Nicros
  • 5,031
  • 12
  • 57
  • 101
  • 2
    What about custom css for `print` media? http://www.w3.org/TR/CSS2/media.html – Mark Meyer Nov 08 '13 at 17:54
  • Maybe I'm missing something but this just allows me to style my print page? I still need to build the html from what angularjs created in the template div and insert it into a new page. At that point, maybe media may come in handy. – Nicros Nov 08 '13 at 18:24
  • The css for print will be applied on the current page with the current dom tree (i.e what angular as rendered) as soon as the user will decide to print. Meaning that in most cases you just need to create your css for print and you are already all set. – Nicolas ABRIC Nov 08 '13 at 18:35
  • Okay, I see what you mean now. I'm using bootstrap 3, so adding the `.hidden-print` class to dom elements I dont want to show works great. But I still need to figure out how to print my tables in a WYSIWIG manner – Nicros Nov 08 '13 at 19:16
  • Well, Nicolas ABRIC or NuclearGhost, one of you should answer this so I can give you the points- the media print stuff, in combination with bootstrap 3's css for media worked great. – Nicros Nov 08 '13 at 19:33
  • @Nicros I converted my comment to an answer – Mark Meyer Nov 08 '13 at 19:36

1 Answers1

0

Instead of trying to create a whole new page, why not simply use specific css for print media.

As always the w3 is the best place to start http://www.w3.org/TR/CSS2/media.html

Mark Meyer
  • 3,643
  • 3
  • 23
  • 34