Nruab mPDF tsis muaj composer ntawm kev pab – Cov ntaub ntawv hauv PDF nrog PHP

Cov mPDF generates PDF ntawm HTML/CSS uas tsis tshua muaj kev nkhaus. Tsab cai idea no yog kom mPDF ua hauj lwm tsis muaj rau nruab lub composer rau cov neeg. Lub composer yuav muab coj los xa mus rau tom ntej.

XIM: Se você não quer instalar o composer de forma alguma, este tutorial não é para você. Neste post será utilizado o composer localmente e posteriormente enviado para o servidor, sem instalação.

Taw qhia

Hnub no kuv tsis pom tau kuv tus kheej programming yam tsis siv lub Composer. Nws tso cai rau koj mus li qhia siv kuj tswj heev heev modules/scripts yog peb ob tog thiab muaj nyob rau hauv https://packagist.org/.

Nrog cov Composer Koj yuav tsis zoo xwb nruab thiab hloov tej pob khoom tau yooj yim, tiam sis tseem nco synchronized muaj dependencies yuav mus lwm pob zaub mov uas siv nyob rau hauv daim ntawv thov. tsis tas li ntawd, twb muaj npaj lub AutoLoad, tas koj lub neej koj PHP kawm tsis xam/yuav tsum.

Tab sis qhov no yog tsis ib qho hais txog Composer :P wb saib yuav ua li cas rau nruab thiab khiav lub mPDF, lub tsev qiv ntawv PHP rau generating PDF siv cov HTML code. Tus mPDF muaj koj cons, tab sis qhov nthuav tshaj plaws yog tias koj yeej yog tas mount koj HTML thiab muaj dag zog tsawg heev twb yuav tau hloov mus PDF.

Teeb meem

No mas, nyob rau hauv lub versions tshiab, Nws yuav siv lub Composer rau nruab ib lub mPDF. Li ntawd, qhov pom zoo yog tias koj tau siv siv lub Composer nyob rau hauv koj lub cev kev loj hlob.

Li no cov nqe lus nug: Yuav ua li cas yog tias kuv tsis muaj tau li cas rau nruab Composer rau cov neeg rau zaub mov? Qhov no, Peb mam li siv ib gimmick: nruab ib lub Composer zos thiab mam li nce (upload) rau cov ntawv tais ceev tseg neeg rau zaub mov “Vendor“. Raws li cov lus hais saud koj Composer yuav muab cov inoperative, tiam sis tseem yuav ua tau siv lub mPDF.

Tov

Como foi dito no início, a ideia é fazer o mPDF funcionar no servidor sem que seja preciso instalá-lo no Servidor. Qhov no, faremos uma instalação local do composer, e então enviaremos para o servidor.

Cov plahaum ntawm tus mPDF los ntawm Composer yooj yim heev. mus tom qhov chaw https://getcomposer.org/download/, Download tau thiab nruab ib lub Composer.

Thaum twg koj muaj qhov Composer ntsia, tsuas qhib lub Terminal (hais kom ua) txhua tus me nyuam rau npe koj daim ntawv thov (li ntawm www/htdocs los yog /, Piv txwv), thiab ces khiav qhov hais kom ua li nram no:

$ composer yuav tsum tau mpdf/mpdf

Nco ntsoov tias thaum lub hauv paus ntawm koj daim ntawv thov directory yuav tau tsim ib co ntaub ntawv, raws li composer .JSON thiab composer. ntsuas phoo, as Well as cov directory “Vendor /“. Tam sim no nws yog ib qho yooj yim! Koj twb muaj txhua yam koj xav tau rau koj mPDF ua haujlwm.

Muab cov ntawv tais ceev tseg Vendor thiab muab xa mus rau koj cov neeg rau zaub mov. Dua li, muab nws cov hauv paus hniav thiab sau ib daim ntawv index.php (kuj muaj nyob hauv lub hauv paus) sim cov mPDF uas muaj cov cai nram qab no:

1
2
3
4
5
<?PHP
require_once _____ _____ DIR _____ _____ . '/vendor/autoload.php';
$mpdf = tshiab mPDF();
$mpdf->WriteHTML('<H1>nyob zoo, lub ntiaj teb!</H1>');
$mpdf->Tso zis();

Ua li no, Yog hais tias txhua yam mus zoo, yuav tshwm sim li ib PDF sau ntawv “nyob zoo, lub ntiaj teb!“.

Cov ntaub ntawv hauv PDF nrog PHP

Daim ntawv qhia txog tsis yog lub hom phiaj ntawm no ncej, tab sis kuv yuav tsum sau tej yam ntxiv. Yog hais tias koj xav kom muab cov ntaub ntawv, ib txoj kev yog rau qhov embed koj CSS/HTML nyob rau hauv cov ntaub ntawv PHP thiab ho dhau lub chaws mus hauv mPDF ua rau lub zog ua hauj lwm, piv li (Rau cov zis pom hauv daim duab 1 hauv qab):

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
<?PHP
require_once _____ _____ DIR _____ _____ . '/vendor/autoload.php';
ob_start();
?>
<style>
    cov lus, th, TD {
        ciam teb: 1PX khoom dub;
        cev qhuav dej ciam teb: cev qhuav dej;
        padding: 5PX;
    }               
    rooj tr:nth rau me nyuam(khib) {
      tom qab-twg: #EEE;
    }  
    rooj tr:nth rau me nyuam(Txawm) {
      tom qab-twg: #FFF;
    }   
    cov lus thead th {
      tom qab-twg: #CCC;
    } 
    cov lus tfoot td {
      tom qab-twg: #CCC;
    } 
</style> 
<H1>Daim ntawv qhia txog kuv</H1>
<table align="center">
    <THEAD>
        <tr>
            <th>Lub npe</th>
            <th>Hnub nyoog</th>
            <th>Tug</th>
        </tr>
    </THEAD>
    <TFOOT>
        <tr>
            <td colspan="3" align="center">Copyright® 2017</TD>
        </tr>
    </TFOOT>>    
    <TBODY>
        <tr>
            <TD>TAYLOR</TD>
            <td align="center">43</TD>
            <TD >TAYLOR@amail.com</TD>
        </tr>
        <tr>
            <TD>Portela</TD>
            <td align="center">47</TD>
            <TD>portela@bmail.com</TD>
        </tr>        
    </TBODY>
</cov lus>
<?PHP
$HTML = ob_get_contents();
$mpdf = tshiab mPDF;
$mpdf->WriteHTML($HTML);
$mpdf->Tso zis();

Txiv cua.. 1 – PDF zis tej ntaub ntawv uas cov CSS/HTML code (saum) tso npe los ntawm tus mPDF

Obviously koj yuav Teacher zoo dua tus uas muab cov CSS/HTML nyob rau hauv ib kem template ua ntaub ntawv thiab thauj nws mus hu cov mPDF.

Xijpeem, tam sim no nws yog rau koj! Muab tus nyeem Daim ntawv muaj tseeb mPDF thiab nyob rau hauv daim ntawv siv thiab compatibilities.

Tag nrho hits: 17303

sau ntawv cia

Qhov koj email chaw nyob yuav tsis muab luam tawm. Search yog cim muaj *