Print orientation using Javascript, CSS, HTML or PHP


Viewed 4,509 times


Is it possible for me to "pre-set" the orientation of the page, when the user is already printing is marked as landscape? (Landscape)

My case is the following, I’m code similar to this example

I’m exporting my own HTML for Excel and in this part it is all ok, but the user does not want to lose his precious seconds configuring the page there in Excel printer properties, he wants to print the page already oriented as "landscape"(Landscape) has as using some language of those cited ?

  • I do not think it is possible to configure the xls for printing in orientation landscape.

  • What if you use the Phpexcel Library ? Comes with plenty of configuration options, including page orientation.

  • @Diegosouza I saw this solution but I didn’t want to add 228 Files and 54 Folders to my project just because a user wants the page of ladinho....

  • 1

    Does it have to be Excel? Because Voce could make a boot to download excel that would generate your file and another to display in the browser that would use html even, in this could use CSS.

  • @Guilhermenascimento does not need to be in excel, I ended up opting for another solution even, thanks

3 answers


If the server platform is Windows. What you can do is create a macro in VBS to do this. Create the file: Worksheet.vbs

$file = 'seu_arquivo.xls';
$path = "C:\\temp\\";
$fp = fopen($path.'worksheet.vbs', 'w');

$content = '
 Sub setUpPage(sheet) 
       With Workbooks("'.$file.'")
          .Sheets (sheet).PageSetup
          .Orientation = xlLandscape 
          .PaperSize = xlPaperA4 
          .Zoom = False
          .FitToPagesWide = 1 
          .FitToPagesTall = False 
       End With 
     End Sub 

fwrite($fp, $content);

Or without passing file, create without writing:

Sub Macro()
    With ActiveSheet.PageSetup
        .Orientation = xlLandscape
    End With
End Sub

And in PHP, run it:

if (file_exists("{$path}worksheet.vbs")) {
    $WshShell = new COM("WScript.Shell");
    $obj = $WshShell->Run("cscript {$path}worksheet.vbs", 0, true); 

Here has more information.

  • I’ll test it as soon as possible

  • Friend sounds like a good answer, but new COM server side wheel and still the server has to be Windows in order to use new COM, AP seems to need this to happen on the client side. See more :)

  • @Sneepsninja Voce want to print on a printer on the server? Or Voce want to print on the client side?

  • @Guilhermenascimento, I agree with you, but it is possible to create a VM on the Linux server for this type of execution, if necessary.


I ended up using the solution of Jasperserver with PHP has a git integration that makes this connection is very simple

an example piece:

try {
    $jasper = new Adl\Integration\RequestJasper();
    To send output to browser
    header('Content-type: application/pdf');
    echo $jasper->run('/reports/samples/AllAccounts');

    To Save content to a file in the disk
    The path where the file will be saved is registered into config/data.ini
    //$jasper->run('/reports/samples/AllAccounts','PDF', null, true);

} catch (\Exception $e) {
    echo $e->getMessage();


I did in CSS once and a JS plugin called printElement.js, however it is not necessary if it is a simple page.

Print Element


@media print{

        size: landscape;

        position: absolute;
        top: 0;
        bottom: 0;
        left: 0;
        right: 0;
        margin: auto;


Hence on the page that will be printed just call the CSS file with:


  • I don’t think it will solve the problem, since it is "exporting to excel", that is, it would solve if it were printing the HTML page.

  • worst I tested and did not solve... nor with the plugin

Browser other questions tagged

You are not signed in. Login or sign up in order to post.