phpword php导出word wordpress实例,设置字体,导出代码等

wordpress代码片段

function content_to_word(){
    global $wpdb;
    date_default_timezone_set('Asia/Shanghai');
    $last_post_id=_cao('last_post_id_setting')!==''?_cao('last_post_id_setting')-1:get_option('last_post_id');//       update_option('last_post_id',1);//  var_dump(get_option('last_post_id'));exit;
    $min_id=!empty($_POST['history_id'])?$_POST['history_id']:$last_post_id;
   
    $post_num=$_POST['num']?$_POST['num']:0;
    $posts_data = $wpdb->get_results("SELECT ID,post_title FROM $wpdb->posts  WHERE post_type= 'post' AND post_status = 'publish'  AND ID > ".$min_id."  order by ID asc ");
    
    $message='';
    $i=$_POST['i']?$_POST['i']:0;
   
    if($i==$post_num){
                    
                  $message=array('code'=>3,'message'=>'<div id="message" class="csf-notice csf-notice-success" style="margin-top:10px" ><p>本次生成任务已执行完,共生成'.$i.'篇文档</p></div>','history'=>$history_id);
                  
                  update_option( 'last_post_id', $_POST['history_id'] );
                  
                 echo json_encode($message);exit;
    }
    
        foreach ($posts_data  as $k=>$v){
           
            $history_id=$v->ID;
           
            //$post_content=$wpdb->get_var("SELECT post_content FROM $wpdb->posts  WHERE ID= '$v->ID'  ");
            
            $post_content = get_post( $v->ID )->post_content;
            
            $post_content=str_replace('[rihide]','',$post_content);
            $post_content=str_replace('[/rihide]','',$post_content);
            
             
            require 'vendor/autoload.php';
            
            
            $phpWord = new \PhpOffice\PhpWord\PhpWord();

            $section = $phpWord->addSection();
           
            $phpWord->setDefaultFontSize('16');//设置文档字体大小
            
           // $myTextElement = $section->addText(strip_tags($post_content));
           
           // $myTextElement->setFontStyle($fontStyle);
            
            $year=date( "Y");
            
            $basic_dir=ABSPATH."wp-content/document/";
            if(!file_exists($basic_dir))
            {
                 
        		@mkdir($basic_dir,0755);
      
        	}
            $upload_dir=$basic_dir.$year;
           
           if(!file_exists($upload_dir))
            {
                 
        		@mkdir($upload_dir,0755);
      
        	}
        
        	$month=date( "m");
            $Dir1=$upload_dir."/".$month."/";
            if(!file_exists($Dir1))
            {
        			@mkdir($Dir1,0755);
        
        	}
	        
            $fileName_1="/".$v->post_title."_".$v->ID."_".date('_YmdHis').".docx";
            $fileName=$Dir1.$fileName_1;
            
            \PhpOffice\PhpWord\Shared\Html::addHtml($section, $post_content, false, false);

            // Save file
            $phpWord->save($fileName);
            
            
        //    $objWriter = \PhpOffice\PhpWord\IOFactory::createWriter($phpWord, 'Word2007');
           // $res=$objWriter->save($fileName);
             
                $i++;
                if(!file_exists($fileName)){
                     $error.='文章生成失败,文章ID:'.$v->ID.'文章标题:'.$v->post_title.',';
                     $message=array('code'=>0,'title'=>$title,'message'=>'<div id="message" class="csf-notice csf-notice-error " style="margin-top:10px"><p>'.$error.'</p></div>');
                     echo json_encode($message);exit;
                     exit;
                }else{
                    update_post_meta( $v->ID, 'cao_status', 1);
                    
                    $down= array(0=>array('name'=>$v->post_title,'url'=>home_url().'/wp-content/document/'.$year.'/'.$month.$fileName_1));
                    update_post_meta( $v->ID, 'cao_downurl_new',$down);
                    $res_data.='文章:"'.$v->post_title.'"已生成文档,ID:'.$v->ID.'。';
                    
                    $message=array('code'=>2,'title'=>$v->post_title,'message'=>'<div id="message"  class="csf-notice csf-notice-success" style="margin-top:10px"><p>文章'.$res_data.'目前共生成'.$i.'篇文档</p ></div>','history'=>$history_id,'i'=>$i,'all'=>count($posts_data));
                     echo json_encode($message);exit;
                 }
              
               
            
        } 
        if(!$message){
               
                $message=array('code'=>1,'title'=>$title,'message'=>'<div id="message" class="csf-notice csf-notice-success" style="margin-top:10px" ><p>数据库中无需要生成文档的文章</p></div>','history'=>$history_id);
                echo json_encode($message);exit;
        }
        
    
    echo json_encode($message);exit;}add_action('wp_ajax_content_to_word', 'content_to_word');


版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容, 请通知我们,一经查实,本站将立刻删除。


客服