Order export pdf file

Posted by argh2xxx on Tue, 21 Apr 2020 18:35:29 +0200

principle

Raw data - > Load template - > temporary file - > PDF file

Scheme preparation

  1. html template to pdf Difficulties: 1. Find a designer for every change; 2. html style is difficult to control (I'm not good at it); 3.html to pdf style is difficult to control
  2. word template to pdf Difficulties: 1. Selection of appropriate plug-ins; 2. Chinese processing

Final selection, word template mode

Implementation steps

  1. WORD plug in Open source address
    composer require phpoffice/phpword

phpword is very easy to use. It supports variables and is very convenient.

  1. WORD to PDF plug-in Download address
/opt/libreoffice6.4/program/soffice
  1. Full demo
    //Generate order pdf
    public function order_pdf_f()
    {
        #1. Prepare order information
        $person_addr = "Beijing";
        $person_name = "Zhang San";
        $person_mobile= "18932000000";

        #2. Generate material file
        $res_dir_path = './res/';
        $templateFile = $res_dir_path.'temp_order.docx';
        $wordPath = $res_dir_path.'tmp/hello.docx';//A file on the computer
        try {
            $templateProcessor = new \PhpOffice\PhpWord\TemplateProcessor($templateFile);
            $templateProcessor->setValue('person_name', $person_name);
            $templateProcessor->setValue('person_addr', $person_addr);
            $templateProcessor->setValue('person_mobile', $person_mobile);
            $templateProcessor->saveAs($wordPath);
        } catch (Exception $e) {
            echo $e->getMessage();
            return;
        }

        #3. Generate pdf and download
        try {
            $pdfPath = $res_dir_path.'tmp';
            $set_charset = 'export LANG=zh_CN.UTF-8;';#linux
            $cmd = $set_charset."\n"."/opt/libreoffice6.4/program/soffice --headless --convert-to pdf:writer_pdf_Export {$wordPath} --outdir {$pdfPath}";

            $status = 1;
            $log = null;
            $log = shell_exec($cmd);
            if(is_null($log)) {
                #echo "start exec";
                exec($cmd.' 2>&1', $log, $status);
            }
            sleep(1);

            #4. Page Jump to download
            $file =$pdfPath."/hello.pdf";//A file on the computer
            $fileName = basename($file);//Get file name
            header("Content-Type:application/octet-stream"); //Tell the browser the document type (mime type); octet stream refers to binary file type; download any type of file can be specified in this way
            header("Content-Disposition:attachment;filename=".$fileName); //Tell the browser to treat the file as an attachment (i.e. download the file); and set the downloaded file name
            header("Accept-ranges:bytes"); //Tell browser the unit of file size
            header("Accept-Length:".filesize($file)); //Tell browser the size of the file
            $h = fopen($file, 'r'); //Open file
            echo fread($h, filesize($file));
        } catch (Exception $e) {
            echo $e->getMessage();
            return;
        }
    }

Be careful

When exporting, there are always garbled codes. At first, it's crazy. I don't know what's wrong. Then I analyzed it calmly.

  1. Data > word template > word file. As long as the generated word file is free of random code, it will be half successful System environment variable modification
#1. Modify locale variables
vi  /etc/locale.conf
LANG=zh_CN.UTF-8

Plug in source code modification

/www/wwwroot/Project name/vendor/phpoffice/phpword/src/PhpWord/TemplateProcessor.php
//Line 256
    /**
     * @param string $subject
     *
     * @return string
     */
    protected static function ensureUtf8Encoded($subject)
    {
        if (!Text::isUTF8($subject)) {
            #$subject = utf8_encode($subject);
            $subject =  iconv("GB2312","UTF-8//IGNORE",$subject);
        }

        return $subject;
    }

  1. word file - > PDF, if there is any garbled code, consider the reason for the font. Install font resolution https://blog.csdn.net/wlwlwlwl015/article/details/51482065

#1. Start using fontconfig to install font library in CentOS 4.x
yum -y install fontconfig

#2. Create Chinese font path
mkdir -p /usr/shared/fonts/chinese

#3. ftp upload font to / usr/shared/fonts/chinese
c Under the plate Windows/Fonts Catalog

#4. Modify font permission
chmod -R 755 /usr/share/fonts/chinese

#5. Install ttmkfdir to search all font information in the directory
yum -y install ttmkfdir

#6. Execute the ttmkfdir command
ttmkfdir -e /usr/share/X11/fonts/encodings/encodings.dir

#7. Introduce Chinese
vi /etc/fonts/fonts.conf
...
<dir prefix="xdg">fonts</dir>
<dir>/usr/share/fonts/chinese</dir> #Add this line
...

#8. Refresh font cache in memory
fc-cache

#9. Check whether it is effective
fc-list

Topics: Programming yum Linux PHP CentOS