如何使用Yii直接从打印机打印?

oxf4rvwz  于 2022-11-09  发布在  其他
关注(0)|答案(3)|浏览(187)

如何直接从打印机打印?
这是一个内部网的Web应用程序。用户将使用这个应用程序,他们将从他们部门的打印机打印出报告(不同的部门,不同的打印机)。我在谷歌上搜索直接从打印机打印出来,但我找不到任何地方。我使用的是Yii Framework 1.11。
我该怎么做?有没有可能直接从打印机打印出来?怎么做?

idv4meu8

idv4meu81#

你可以试试这个:-

$fhandle = fopen("test.php","rb");
$contents = fread($fhandle, filesize("test.php"));
$output = eval($contents);

$handle = printer_open("Dell Laser Printer M5200");
printer_set_option($handle,PRINTER_MODE,"raw");
printer_write($handle,$output);
printer_close($handle);
bmvo0sr5

bmvo0sr52#

对于Web应用程序,所有的打印工作都依赖于您的浏览器。您必须创建一个页面或弹出窗口,只为打印视图与特殊的Html和css。

1sbrub3j

1sbrub3j3#

至少我找到了最好的方法。我从下面的网站得到了js。
http://www.position-absolute.com/articles/printing-web-pages-a-new-jquery-printing-plugin/
我的视图(索引或其他内容)

<p align="right">
        <?php 

            echo CHtml::link('New Job',array('create'),array('class'=>'btn btn-info'));
            echo CHtml::link('Print',array('print'),array('class'=>'btnPrint btn btn-info', 'style'=>'margin-left: 10px;'));
        ?>
    </p>
<?php $this->widget('zii.widgets.grid.CGridView', array(
    'id'=>'job-grid',
    'dataProvider'=>$jobs->search(),
    'filter'=>$jobs,
...)); ?>

我的控制器

public function actionPrint() {
        $d = $_SESSION['all'];
        $this->renderPartial('print',array('d'=>$d),false,true);
}

我的模型

public function search()
    {
        // Warning: Please modify the following code to remove attributes that
        // should not be searched.

        $criteria=new CDbCriteria;

        $criteria->compare('id',$this->id,true);
        $criteria->compare('name',$this->name,true);
        $criteria->compare('date',$this->date,true);
        $criteria->compare('department_id',$this->department_id);
        $criteria->compare('section_id',$this->section_id);
        $criteria->compare('team_id',$this->team_id);
        $criteria->compare('created_date',$this->created_date,true);
        $criteria->compare('updated_date',$this->updated_date,true);

        $data = new CActiveDataProvider(get_class($this), array(
                'criteria'=>$criteria,
                'pagination'=>false,
        ));
        $_SESSION['all'] = $data;

        $data = new CActiveDataProvider(get_class($this), array(
                'pagination'=>array('pageSize'=> Yii::app()->user->getState('pageSize',
                        Yii::app()->params['defaultPageSize']),),
                'criteria'=>$criteria,
        ));
        $_SESSION['limited'] = $data;

        return $data;
    }

我的视图(print.php)

<div id="summary">
<table width="100%" border="1">
    <tr>
        <th>No.</th>
        <th>Job No.</th>
        <th>Date</th>
        <th>Department</th>
        <th>Section</th>
        <th>Team</th>
    </tr>  
    <?php   
    $i=1;
    foreach($d->data as $item)
    {
    ?>
    <tr>
        <td><?php echo $i;?></td>
        <td><?php echo $item->name; ?></td>
        <td><?php echo $item->date; ?></td>
        <td><?php echo $item->departmentsdep->name; ?></td>
        <td><?php echo $item->departmentssec->name; ?></td>
        <td><?php echo $item->departmentstea->name; ?></td>
    </tr>
    <?php 
        $i++;
    }
    ?>
</table>
</div>

相关问题