通过AJAX请求生成打印页面上的样式

I use AJAX to simply generator, but i have small problem with styles. I don't find solution of my problem.

I sending request into PHP file with AJAX, this returns HTML code. I getting on javascript and generating "print page". If page doesn't have styles - all is okey, but when i try adding styles with this method, script return white page without content.

$.post({
    type: "POST",
    url: "generate.php",
    data: {pid: pid},
}).done(function(data) {
    output = window.open('');
    output.document.write('<html><head><link rel="stylesheet" type="text/css" href="offer.css" /></head><body>');
    output.document.write(data + '</body></html>');
    output.print();
});

PHP code just like this

<?php
$foo = "foo";
ob_start();
?>

<p><?php echo $foo; ?></p>

<?php 

$result = ob_get_flush();
echo $result;

?>

If write() is used after an HTML document is fully loaded it deletes all existing HTML. So for multiple lines try it like this:

document.write(
   '<html><head><link rel="stylesheet" type="text/css" href="offer.css" /></head><body>'
   + data + '</body></html>'
)