下载PDF功能可在某些页面上打印白页

时间:2017-01-27 08:53:05

标签: javascript php jquery pdf

我制作了一个打印功能,它几乎适用于每一页,但例如在这个页面打印5个白页?

enter image description here

它仅在第一页显示类别和标题,内容在第5页后显示。其他页面是在标题和类别之后。我找不到可以帮助我的解决方案吗?

以下是我在某些页面上打印PDF的代码。

function generate_project_pdf($postId){
    $query = new WP_Query(array('post__in' => array($postId), 'post_type' => 'projecten', 'post_status' => 'publish'));

    while($query->have_posts()){
        $query->the_post();

        ob_start();?>

        <?php
            $categories = get_the_terms($postId, 'Werkgebied');

            $primaryCat = new WPSEO_Primary_Term('Werkgebied', $postId);
            $primaryCat = $primaryCat->get_primary_term();

            $primaryCatName = '';
            $arOtherCateg = array();
            foreach($categories as $category) {
                if($primaryCat) {
                    if($primaryCat == $category->term_id){
                        $primaryCatName = $category->slug;
                    } else {
                        $arOtherCateg[] = $category->slug;
                    }
                } else {
                    $arOtherCateg[] = $category->slug;
                }
            }
            $terms1 = $arOtherCateg;
            if($primaryCatName)
                $terms1[] = $primaryCatName;


            ?>

    <div class="wrapper">
        <div class="content_block content_block_top" role="main">
            <div class="cat_corner">
                <table cellspacing="0" cellpadding="0">
                    <tr>
                    <?php foreach($terms1 as $category) { echo '<td class="'.$category.'"><div>'.$category . '</div></td>'; } ?>
                    </tr>
                </table>

            </div>

            <article>
                <div style="">
                    <p style="font-family: 'Open Sans';font-size: 45px; line-height: 50px;padding: 50px 0px 20px 0px; margin: 0px; color: #009fca;"><?php echo get_the_title($postId); ?></p>
                </div>
                <table width="100%" cellspacing="0" cellpadding="0">
                    <tr>
                        <td width="77%" valign="top" style="padding-right: 30px"><?php the_content(); ?></td>
                        <td style="padding-top: 25px;" valign="top">
                            <div class="projecten_info">
                                <div class="contactpersoon">
                                    <?php if( get_post_meta($postId, 'profile_pic', true) ) { ?>
                                        <img src="<?php echo get_field('profile_pic', $postId); ?>" alt="" />
                                    <?php } ?>
                                    <div class="h4">Contactpersoon</div>
                                    <?php if( get_post_meta($postId, 'profile_name', true) ) { ?>
                                        <p><?php echo get_field('profile_name', $postId); ?></p>
                                    <?php } ?>
                                    <?php if( get_post_meta($postId, 'profile_email', true) ) { ?>
                                        <a href="mailto:<?php echo get_field('profile_email', $postId); ?>">mail</a>
                                    <?php } ?>
                                </div><br>
                                <?php if( get_post_meta($postId, 'project_details', true) ) { ?>
                                <div class="details">
                                    <div class="h4">Project details</div>
                                    <?php echo get_field('project_details', $postId); ?>
                                </div>
                                <?php } ?>
                            </div>
                        </td>
                    </tr>
                </table>
            </article>
        </div>
    </div>
    <?php if( have_rows('timeline', $postId) ): ?>
    <!-- timeline -->
    <div class="timeline">
        <div class="wrapper">
            <ul class="tab_select clear">
            </ul>
            <?php while( have_rows('timeline', $postId) ): the_row();
                $date = get_sub_field('date');
                $content = get_sub_field('content');
            ?>
                <div class="tab_content" id="<?php echo $date; ?>">
                    <?php echo $content; ?>
                </div>
            <?php endwhile; ?>
        </div>
    </div>
    <!-- /timeline -->
    <?php endif; ?>


    <?php if( have_rows('slider', $postId) ): ?>
    <!-- slider -->
    <div class="slider">
        <ul class="clear">
            <?php while( have_rows('slider', $postId) ): the_row();
                $slide = get_sub_field('slide');
            ?>
            <li data-cover=".cover"><img class="cover" src="<?php echo $slide; ?>" alt="" width="100%"/></li>
            <?php break; endwhile; ?>
        </ul>
    </div>
    <!-- /slider -->
    <?php endif; ?>


    <?php if( get_post_meta($postId, 'additional_content', true) ) { ?>
    <div class="wrapper">
        <!-- content_block -->
        <div class="content_block">
            <?php
                $field = get_field('additional_content', $postId);
                preg_match_all('/<img([^>])+>/', $field, $arMatches);
                if(count($arMatches)) {
                    $img = $arMatches[0][0];
                    echo str_replace($img, '', $field).'<br>'.$img;
                }
                else
                    echo $field;
                ?>
        </div>
        <!-- /content_block -->
    </div>
    <?php } ?>

    <?php
        }
        $html =
    '<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">

        <head>
        <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
        <link rel="stylesheet" href="http://fonts.googleapis.com/css?family=Open+Sans:400,400italic,600,700" type="text/css" >
            <style type="text/css">
                @page { margin: 0px; }
                body { margin: 0px; font: 16px \'Open Sans\', sans-serif; line-height: 1.3; color: #564f43;}
                p {  padding-bottom: 10px;}
                .slider ul, .slider li {list-style: none; padding: 0px; margin: 0px;}
                .wrapper { padding: 20px 30px 30px 30px;}

                .content_block .cat_corner { position: absolute; top: 0; right: 0; font-size: 20px; color: #fff;}

                .content_block .cat_corner div { margin-left: 10px; padding: 5px 15px;}
                .content_block .cat_corner .infra div {background:#068dbd;}
                .content_block .cat_corner .bagger div {background:#453d32;}
                .content_block .cat_corner .milieutec div{background:#ffc900;color:#574f44;}
                .content_block .cat_corner .groen div{background:#96c73c;}
                .single-projecten .projecten > header { padding-bottom: 0px;}
                .single-projecten .projecten_info { width: 100%;}
                .single-projecten .projecten_info .contactpersoon { background: #f6f6f6; position: relative; padding: 20px 25px 10px 125px; margin-bottom: 10px;}
                .single-projecten .projecten_info .contactpersoon img { position: absolute; top: 20px; left: 25px; width: 80px;}
                div.h4 { font: 21px "Open Sans"; color: #009fca; padding-bottom: 0px;}
                .single-projecten .projecten_info .contactpersoon h4 { padding-bottom: 0px; font: 21px "Open Sans"; color: #009fca;padding-top: 0px;}
                .single-projecten .projecten_info .contactpersoon p { padding-bottom: 10px;}
                .single-projecten .projecten_info .contactpersoon > a {text-align: right; color: #009fca; padding-right: 30px;
                    background: url('.THEME_URL.'/images/icons/arrow6.png) right center no-repeat; text-decoration: none;}
                .single-projecten .projecten_info .details { background: #f6f6f6; position: relative; padding: 20px 25px 15px 25px; margin-bottom: 10px;}
                .single-projecten .projecten_info .details p {padding-bottom: 10px;}
                h2 { font: 33px "Open Sans"; padding-bottom: 20px;}
                .content_block ul li.content_block ul, .content_block ul li {list-style: none;}
                .content_block ul li { padding-left: 30px;background: url('.THEME_URL.'/images/icons/list_item.png) 0 5px no-repeat;}
                .content_block ul li:nth-child(4n-2) {background-image: url('.THEME_URL.'/images/icons/list_item2.png);}
                .content_block ul li:nth-child(4n-1) {background-image: url('.THEME_URL.'/images/icons/list_item3.png);}
                .content_block ul li:nth-child(4n) {background-image: url('.THEME_URL.'/images/icons/list_item4.png);}
            </style>
        </head>
        <body class="single-projecten">
            '.ob_get_contents().'
        </body>
    </html>';

    ob_end_clean();

    include_once(get_template_directory().'/dompdf/dompdf_config.inc.php');

    $dompdf = new DOMPDF();
    $dompdf->set_paper('A4');
    $dompdf->load_html($html);
    $dompdf->render();
    $output = $dompdf->output();

    file_put_contents(get_template_directory() ."/pdfs/".$postId.".pdf", $output);
}
add_action("wp_ajax_generate_project_pdf", "download_project_pdf");
add_action("wp_ajax_nopriv_generate_project_pdf", "download_project_pdf");

function download_project_pdf() {

    $postId = $_GET['id'];
    if(!file_exists(get_template_directory() ."/pdfs/".$postId.".pdf"))
        generate_project_pdf($postId);

    header('Set-Cookie: fileDownload=true; path=/');
    header('Cache-Control: max-age=60, must-revalidate');
    header("Content-type: application/pdf");
    header('Content-Disposition: attachment; filename="'.get_the_title($postId).'.pdf"');
    readfile(get_template_directory() ."/pdfs/".$postId.".pdf");

    exit;

}

0 个答案:

没有答案