dongzhoutuo0883 2015-10-01 11:07
浏览 34

使用renderPartial以.docx格式保存html

I'm trying to download the rendered HTML in .docx format. Its working fine when I try to download in .doc format. I need to download file in .docx format instead of .doc

Below is my code when I try to download in .doc format:

header('Content-Type: application/octet-stream');
header("Content-Disposition: attachment;filename=someName.doc");
header('Cache-Control: max-age=0');
header_remove('Pragma');

$this->renderPartial('viewExport', array(
      'briefingId' => $briefingId
));

Below is my code when I try to download in .docx format:

header('Content-Type: application/vnd.openxmlformats-officedocument.wordprocessingml.document');
header("Content-Disposition: attachment;filename=someName.docx");
header('Cache-Control: max-age=0');
header_remove('Pragma');
$this->renderPartial('viewExport', array(
       'briefingId' => $briefingId
));

Here is my ViewExport code:

<style type="text/css">
    .container {
        margin-left: auto;
        margin-right: auto;
        width: 940px;
    }

    .table th, .table td {
        line-height: 20px;
        text-align: left;
        padding: 8px;
    }

    .table td {
        border: 0;
    }

    ul {
        font-size: 12px;
        font-weight: 400;
        font-style: normal;
    }

    p {
        margin: 0;
    }

    li.name {
        font-size: 10pt;
        margin-bottom: 10px;
    }

    body {
        font-family: Arial, Helvetica, sans-serif;
        font-size: 12px;
        line-height: 18px;
    }

    .center {
        font-family: Arial, Helvetica, sans-serif;
    }

    .briefing-info, {
        font-family: Arial, Helvetica, sans-serif;

    }

    table.agenda > tr > td {
        padding-bottom: 1em;
    }

</style>


<?php
$agendaAttendeeInfo = AgendaItem::model()->getAgendaExportData($briefingId);
$this->renderPartial('view', $agendaAttendeeInfo);
?>

Here is my view code:

<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<?php $wordForBriefing = HtmlHelper::encodeForXss(Yii::app()->settings->get("briefing", "wordForBriefing")); ?>
<style type="text/css">
    .header {
        padding-top: 10px;
        padding-bottom: 10px;
    }

    .left-logo, .right-logo {
        width: 200px;
    }

    .left-logo img, .right-logo img {
        vertical-align: top;
    }

    .right-logo {
        text-align: right;
    }

    .center-head {
        text-align: center;
        color: #808080;
        font-size: 16pt;
        font-family: Arial, Helvetica, sans-serif;
    }

    .briefing-info {
        color: #808080;
        font-size: 10pt;
        font-weight: bold;
        font-family: Arial, Helvetica, sans-serif;
    }

    .attendee-header {
        color: #000000;
        font-size: 12pt;
        font-weight: bold;
    }

    .table td {
        border: 0;

    }

    .job-title {
        font-style: italic;
        color: #808080;
        font-size: 10pt;
    }

    .name {
        font-size: 10pt;
        padding-bottom: 10px;
    }

    .other-than-topic {
        font-weight: bold;
        font-style: italic;
    }

    p {
        margin: 0;
    }

    .name .time {
        width: 200px;
    }

    .name .topic {
        width: 380px;
    }

    .name .speaker {
        width: 330px;
    }
</style>
<div class="container">
<?php if(Yii::app()->settings->get("agenda", 'addInternalAttendeeListToAgenda') || Yii::app()->settings->get("agenda", 'addExternalAttendeeListToAgenda')) { ?>
    <div class='header'>
        <table width="100%">
            <tr>
                <td class='left-logo'>
                    <?php
                    $imageUrl = Yii::getPathOfAlias('application.images') . "/logo.png";
                    $imageDimensions = Util::calculateImageDimensions($imageUrl);
                    $logo = Yii::app()->getBaseUrl(true) . "/images/logo.png";
                    echo CHtml::image($logo, '', array('height' => $imageDimensions[1], 'width' => $imageDimensions[0]));
                    ?>                </td>
                <td class='center-head'>
                    Welcome<br/>
                    <span style="font-weight: bold">
                        <?php
                        if ($customerCompany != '')
                            echo $customerCompany;
                        ?>
                    </span>
                </td>
                <td class='right-logo'>
                    <?php
                    if (!empty($briefing['customer_logo'])) {
                        $imageUrl = Yii::app()->settings->get('upload', 'baseUploadPath') . 'briefings/' . $briefing['customer_logo'];
                        $imageDimensions = Util::calculateImageDimensions($imageUrl);
                        $customerLogo = $this->createAbsoluteUrl('briefing/downloadUploadedFile', array('fileName' => $briefing['customer_logo']));
                        echo CHtml::image($customerLogo, '', array('height' => $imageDimensions[1], 'width' => $imageDimensions[0]));
                    }
                    ?>
                </td>
            </tr>
        </table>
    </div>
    <div style="margin-bottom: 20px">
        <div align="center" class="briefing-info">
            <?php
            if ($briefing['scheduled_date'])
                echo date('l, F d, Y', strtotime($briefing['scheduled_date']));
            ?> <br>
            <?php echo $wordForBriefing; ?> Room:
            <?php
            if ($briefing['roomName'])
                echo $briefing['roomName'];
            ?> <br>
            <?php echo $wordForBriefing; ?> Number:
            <?php echo $briefing['id']; ?>
        </div>
    </div>
<?php } ?>
<table class='table'>
    <tbody>

    <?php if ($externalAttendees && Yii::app()->settings->get("agenda", 'addExternalAttendeeListToAgenda')) { ?>
        <?php foreach ($externalAttendees as $company => $attendees) { ?>
            <tr>
                <td class="attendee-header">
                    <p>
                        <?php echo $company . " - Attendees" ?>
                    </p>
                </td>
            </tr>
            <tr>
                <td>
                    <ul>
                        <?php foreach ($attendees as $attendee) { ?>
                            <?php
                            $attendeeName = $attendee['name'];
                            $attendeeName .= empty($attendee['job_title']) ? "" : ", <span class='job-title'>" . $attendee['job_title'] . "</span>";
                            echo CHtml::openTag('li', array('class' => 'name')) . $attendeeName . CHtml::closeTag('li');
                            ?>
                        <?php } ?>
                    </ul>
                </td>
            </tr>
        <?php } ?>
    <?php }

    if(Yii::app()->settings->get("agenda", 'addInternalAttendeeListToAgenda')) {
        ?>

        <tr>
            <td class="attendee-header">
                <p><?php echo Yii::app()->settings->get('general', 'companyName'); ?> - Attendees</p>
            </td>
        <tr>
            <td>
                <ul>
                    <?php
                    if ($internalAttendees) {
                        foreach ($internalAttendees as $attendee) {
                            $attendeeName = $attendee['name'];
                            $attendeeName .= empty($attendee['job_title']) ? "" : ", <span class='job-title'>" . $attendee['job_title'] . "</span>";
                            echo CHtml::openTag('li', array('class' => 'name')) . $attendeeName . CHtml::closeTag('li');
                        }
                    } else
                        echo "No Attendees";
                    ?>
                </ul>
            </td>
        </tr>
    <?php } ?>
    </tbody>
</table>

<?php
$briefingRooms = BriefingRoom::model()->forBriefing($briefing['id'])->findAll();
$briefingRooms = CHtml::listData($briefingRooms, 'briefing_date', 'room.name');

$briefingModel = Briefing::model()->findByPk($briefing['id']);
$briefingDuration = $briefingModel->getTotalBriefingDuration();

foreach($briefingDuration as $date => $briefingData){
    ?>
    <br clear=all style='mso-special-character:line-break;page-break-before:always'>
    <div>
        <table width="100%">
            <tr>
                <td class='left-logo'>
                    <?php
                    $imageUrl = Yii::getPathOfAlias('application.images') . "/logo.png";
                    $imageDimensions = Util::calculateImageDimensions($imageUrl);
                    $logo = Yii::app()->getBaseUrl(true) . "/images/logo.png";
                    echo CHtml::image($logo, '', array('height' => $imageDimensions[1], 'width' => $imageDimensions[0]));
                    ?>
                </td>
                <td class='center-head'>
                    Welcome <br/>

                    <div>
                            <span style="font-weight: bold">
                            <?php
                            if ($customerCompany != '')
                                echo $customerCompany;
                            ?>
                            </span>
                    </div>
                </td>
                <td class='right-logo'>
                    <?php
                    if (!empty($briefing['customer_logo'])) {
                        $imageUrl = Yii::app()->settings->get('upload', 'baseUploadPath') . 'briefings/' . $briefing['customer_logo'];
                        $imageDimensions = Util::calculateImageDimensions($imageUrl);
                        $customerLogo = $this->createAbsoluteUrl('briefing/downloadUploadedFile', array('fileName' => $briefing['customer_logo']));
                        echo CHtml::image($customerLogo, '', array('height' => $imageDimensions[1], 'width' => $imageDimensions[0]));
                    }
                    ?>
                </td>
            </tr>
        </table>
    </div>
    <div style="margin-bottom: 20px">
        <div align="center" class="briefing-info">
            <?php
            if ($briefingData['scheduled_date']) {
                $date = new DateTime($date);
                echo $date->format("l, F d, Y");
            }
            ?> <br>
            <?php echo $wordForBriefing; ?> Room:
            <?php
            echo (isset($briefingRooms[$date->format("Y-m-d")])) ? $briefingRooms[$date->format("Y-m-d")] : $briefing['roomName'];
            ?> <br>
            <?php echo $wordForBriefing; ?> Number:
            <?php echo $briefing['id']; ?>
        </div>
    </div>
    <table class="agenda">
        <?php
        foreach ($agendaItems as $agendaItem) {
            $agendaDate = new DateTime($agendaItem['agenda_date']);
            ?>
            <?php if ($agendaDate == $date) { ?>
                <tr class="name">
                    <td style="vertical-align: top;" class="time">
                        <?php echo date("h:i a", strtotime($agendaItem['start_time'])); ?>
                    </td>
                    <td style="vertical-align: top;" class="topic">
                        <?php

                        if ($agendaItem['type'] != AgendaItem::TYPE_TOPIC && $agendaItem['type'] != AgendaItem::TYPE_CONFERENCING) {
                            ?>
                            <span class='other-than-topic'>
                        <?php
                        if (empty($agendaItem['display_title']) && empty($agendaItem['meal_type'])) {
                            echo ucfirst(strtolower($agendaItem['type']));
                        } else if (empty($agendaItem['display_title'])) {
                            echo trim($agendaItem['meal_type']);
                        } else {
                            echo trim($agendaItem['display_title']);
                        }
                        ?>
                        </span>
                        <?php
                        } else {
                            echo empty($agendaItem['display_title']) ? trim($agendaItem['topic']) : trim($agendaItem['display_title']);
                        }
                        ?>
                    </td>
                    <td style="vertical-align: top;" class="speaker">
                        <?php
                        $agendaItemSpeakersWithTitles = AgendaItemSpeaker::model()->getAgendaItemSpeakersForAgenda($agendaItem['agendaId']);
                        $speakers = array();
                        foreach ($agendaItemSpeakersWithTitles as $speaker) {
                            $speakerNameWithTitle = $speaker['name'];
                            $speakerNameWithTitle .= empty($speaker['job_title']) ? '' : ', <span class="job-title">' . $speaker['job_title'] . '</span>';
                            $speakers[] = $speakerNameWithTitle;
                        }
                        echo implode("<br/>", $speakers);
                        ?>
                    </td>
                </tr>
            <?php } ?>
        <?php } ?>
    </table>
<?php } ?>
<?php $agendaCount = isset($agendaItems) ? count($agendaItems) : 0; ?>
</div>

<script type="text/javascript">
    window.onload = function () {
        var print = <?php echo isset($_GET['print']) ? $_GET['print'] : 0; ?>;
        if (print == 1) {
            window.print();
        }
    };
</script>
  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥15 如何用stata画出文献中常见的安慰剂检验图
    • ¥15 c语言链表结构体数据插入
    • ¥40 使用MATLAB解答线性代数问题
    • ¥15 COCOS的问题COCOS的问题
    • ¥15 FPGA-SRIO初始化失败
    • ¥15 MapReduce实现倒排索引失败
    • ¥15 ZABBIX6.0L连接数据库报错,如何解决?(操作系统-centos)
    • ¥15 找一位技术过硬的游戏pj程序员
    • ¥15 matlab生成电测深三层曲线模型代码
    • ¥50 随机森林与房贷信用风险模型