最近よく使う方法ですが、以下のファイルはprint.phpです。
xmlをサポートしているブラウザ(Firefox,Chrome)などで下記のコマンドを実行してください。
https://system-can.com/support/example/dpandpr.php?m=1
下記PHPのfunction b()で表示します。プレビューのような感じでデバッグが簡単でしょう?
パソコンに印刷ソフトPXdoc3をダウンロードしているならm=0で印刷ダイアログが表示し印刷を開始します。
プログラムから呼ぶ場合も同じです。
https://system-can.com/support/example/dpandpr.php?m=0
ご注意:PXDoc3はXMLと完全な互換ではありません!画面に表示されても印刷エラーになる場合がありますのでご注意ください。
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 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 | <?php /* PHP 印刷サンプル dpandpr.php */ session_start(); date_default_timezone_set( 'Asia/Tokyo' ); function a(){ global $fl_db , $pxd ; echo '<!DOCTYPE html> <html lang= "ja" > <head> <meta charset= "utf-8" > <meta http-equiv= "Pragma" content= "no-cache" > <meta http-equiv= "Cache-Control" content= "no-cache" > <meta http-equiv= "Expires" content= "0" > <title> print </title> <script src= "../js/pxd3.js" ></script> <script type= "text/javascript" > function on(){ pxd.printout(\'dpandpr.php?m=1\'); } </script> </head> <body onload= "on()" > </body> </html>'; } // function b(){ $d = array (); //サンプルなので配列は空に 実際はSESSONなどで $attach_data ='<?xml version= "1.0" encoding= "utf-8" ?> <pxd paper-type= "A4" orientation= "portrait" title= "請求書" xmlns:xlink= "https://www.w3.org/1999/xlink" name= "Pxd" > <page> <svg x= "0" y= "0" width= "210mm" height= "297mm" viewBox= "0 0 2100 2970" > <!-- 角丸長方形 --> <!-- 外側 --> <rect x= "150" y= "150" width= "1800" height= "2670" stroke= "black" rx= "5" stroke-width= "1" fill= "none" /> <!-- 金額のかこみ --> <rect x= "200" y= "620" width= "800" height= "130" stroke= "black" rx= "5" stroke-width= "1" fill= "gainsboro" /> <!-- 名前のLINE --> <line x1= "200" y1= "510" x2= "1000" y2= "510" stroke= "black" stroke-width= "1" /> <!-- 自社名のLINE --> <line x1= "1200" y1= "690" x2= "1880" y2= "690" stroke= "black" stroke-width= "1" /> <rect x= "1700" y= "720" width= "150" height= "150" stroke= "black" rx= "2" stroke-width= "1" fill= "none" /> <rect x= "1500" y= "720" width= "150" height= "150" stroke= "black" rx= "2" stroke-width= "1" fill= "none" /> <rect x= "1300" y= "720" width= "150" height= "150" stroke= "black" rx= "2" stroke-width= "1" fill= "none" /> <!-- データ部 --> <!-- 請求書No --> <text x= "1920" y= "210" font-size= "36" text-anchor= "end" > '.$d[1].' </text> <!-- 日付 --> <text x= "1920" y= "260" font-size= "36" text-anchor= "end" > '.$d[2]."年".$d[3]."月".$d[4]."日".' </text> <!-- 領収書 --> <text x= "1050" y= "220" font-size= "60" text-anchor= "middle" font-weight= "bold" > '."ご 請 求 書".' </text> <!-- 宛先お名前 --> <text x= "600" y= "490" font-size= "50" text-anchor= "middle" > '.$d1[1].' </text> <text x= "200" y= "600" font-size= "30" text-anchor= "top" >ご請求金額</text>'; //---- 合計金額 ---- if ( $d [18]== "checked" ) { //外税 $attach_data .= '<text x="600" y="720" font-size="80" text-anchor="middle">' . "\\" .number_format( $d2 [7]+ $d2 [8]). "-" . '</text>' ; } else { //内税 $attach_data .= '<text x="600" y="720" font-size="80" text-anchor="middle">' . "\\" .number_format( $d2 [7]). "-" . '</text>' ; } //---- 自社名住所 ----- $attach_data .='<!-- 社名 --> <text x= "1200" y= "450" font-size= "60" text-anchor= "top" > '.$d[5].' </text> <text x= "1200" y= "500" font-size= "36" text-anchor= "top" > '.$d[6].' </text> <text x= "1200" y= "540" font-size= "30" text-anchor= "top" > '.$d[7].' </text> <text x= "1200" y= "580" font-size= "36" text-anchor= "top" > '.$d[8].' </text> <text x= "1200" y= "620" font-size= "36" text-anchor= "top" > '.$d[9].' </text>'; //-----電話 FAX------- if ( strlen ( $d [10])) { $attach_data .= '<text x="1200" y="660" font-size="36" text-anchor="top">TEL ' . $d [10]. "-" . $d [11]. "-" . $d [12]. '</text>' ; } if ( strlen ( $d [13])) { $attach_data .= '<text x="1550" y="660" font-size="36" text-anchor="top">FAX ' . $d [13]. "-" . $d [14]. "-" . $d [15]. '</text>' ; } //------詳細項目------ $attach_data .=' <text x= "200" y= "1030" font-size= "36" text-anchor= "top" >項目</text> <text x= "600" y= "1030" font-size= "36" text-anchor= "top" >内 容</text> <text x= "1280" y= "1030" font-size= "36" text-anchor= "top" >数量</text> <text x= "1460" y= "1030" font-size= "36" text-anchor= "top" >単 価</text> <text x= "1720" y= "1030" font-size= "36" text-anchor= "top" >金 額</text> <line x1= "190" y1= "1080" x2= "1910" y2= "1080" stroke= "black" stroke-width= "5" /> <line x1= "190" y1= "980" x2= "1910" y2= "980" stroke= "black" stroke-width= "5" /> <!-- 縦 --> <line x1= "290" y1= "980" x2= "290" y2= "2580" stroke= "black" stroke-width= "1" /> <line x1= "1240" y1= "980" x2= "1240" y2= "2580" stroke= "black" stroke-width= "1" /> <line x1= "1400" y1= "980" x2= "1400" y2= "2580" stroke= "black" stroke-width= "1" /> <line x1= "1620" y1= "980" x2= "1620" y2= "2580" stroke= "black" stroke-width= "1" />'; //----詳細データ部---- for ( $i =1; $i <26; $i ++){ $y =1060+ $i *60; $y1 = $y +20; if (mb_strwidth( $dt [2][ $i ], 'UTF-8' )>50) $dt [2][ $i ]=mb_strimwidth( $dt [2][ $i ], 0, 50, "..." ); $attach_data .=' <text x= "205" y= "'.$y.'" font-size= "36" text-anchor= "top" > '.$dt[1][$i].' </text> <text x= "300" y= "'.$y.'" font-size= "36" text-anchor= "top" > '.$dt[2][$i].' </text> <text y= "'.$y.'" font-size= "36" '; if ( is_numeric ( $dt [3][ $i ])) { $attach_data .= 'x="1380" text-anchor="end">' .number_format( $dt [3][ $i ]). '</text>' ; } else { $attach_data .= 'x="1320" text-anchor="middle">' . $dt [3][ $i ]. '</text>' ; } $attach_data .= '<text y="' . $y . '" font-size="36" ' ; if ( is_numeric ( $dt [4][ $i ])) { $attach_data .= 'x="1600" text-anchor="end">' .number_format( $dt [4][ $i ]). '</text>' ; } else { $attach_data .= 'x="1540" text-anchor="middle">' . $dt [4][ $i ]. '</text>' ; } $attach_data .= '<text y="' . $y . '" font-size="36" ' ; if ( is_numeric ( $dt [5][ $i ])) { $attach_data .= 'x="1900" text-anchor="end">' .number_format( $dt [5][ $i ]). '</text>' ; } else { $attach_data .= 'x="1820" text-anchor="middle">' . $dt [5][ $i ]. '</text>' ; } $attach_data .= '<line x1="190" y1="' . $y1 . '" x2="1910" y2="' . $y1 . '" stroke="black" stroke-width="1"/>' ; } if ( $d1 [8]>0) { // 消費税があるなら $attach_data .='<!-- 小計 --> <text x= "1460" y= "2640" font-size= "36" text-anchor= "middle" >小 計</text> <text x= "1900" y= "2640" font-size= "36" text-anchor= "end" > '.number_format($d2[7]).' </text> <line x1= "1400" y1= "2660" x2= "1910" y2= "2660" stroke= "black" stroke-width= "5" /> <!-- 消費税 --> <text x= "1460" y= "2700" font-size= "36" text-anchor= "middle" >消費税</text> <text x= "1900" y= "2700" font-size= "36" text-anchor= "end" > '.number_format($d2[8]).' </text> <line x1= "1400" y1= "2720" x2= "1910" y2= "2720" stroke= "black" stroke-width= "5" /> <!-- 合 計 --> <text x= "1460" y= "2760" font-size= "36" text-anchor= "middle" >合 計</text> <text x= "1900" y= "2760" font-size= "36" text-anchor= "end" > '.number_format($d2[9]).' </text> <line x1= "1400" y1= "2780" x2= "1910" y2= "2780" stroke= "black" stroke-width= "5" />'; } else { $attach_data .='<!-- 合 計 --> <text x= "1460" y= "2640" font-size= "36" text-anchor= "middle" >合 計</text> <text x= "1900" y= "2640" font-size= "36" text-anchor= "end" > '.number_format($d2[9]).' </text> <line x1= "1400" y1= "2660" x2= "1910" y2= "2660" stroke= "black" stroke-width= "5" />'; } if (mb_strwidth( $d1 [10], 'UTF-8' )>50) $d1 [10]=mb_strimwidth( $d1 [10], 0, 50, "..." ); if (mb_strwidth( $d1 [11], 'UTF-8' )>50) $d1 [11]=mb_strimwidth( $d1 [11], 0, 50, "..." ); if (mb_strwidth( $d1 [12], 'UTF-8' )>50) $d1 [12]=mb_strimwidth( $d1 [12], 0, 50, "..." ); $attach_data .='<!-- 備考 --> <text x= "210" y= "2640" font-size= "36" fill= "black" stroke= "none" text-anchor= "top" > '.$d1[10].' </text> <text x= "210" y= "2700" font-size= "36" fill= "black" stroke= "none" text-anchor= "top" > '.$d1[11].' </text> <text x= "210" y= "2760" font-size= "36" fill= "black" stroke= "none" text-anchor= "top" > '.$d1[12].' </text> <!-- 商標権 --> <!-- Copyright (C) 2022 System-CAN All Rights Reserved. --> </svg> </page> </pxd>'; print $attach_data ; } $m =htmlspecialchars( $_GET [ "m" ]); switch ( $m ): case '1' : //表示 b(); break ; default : a(); break ; endswitch ; ?> |
実行テスト
表示 dpandpr.php?m=1
印刷 dpandpr.php?m=0
2022/4/23 修正