teratail header banner
teratail header banner
質問するログイン新規登録

質問編集履歴

2

サンプルコードと出力例を追記

2016/02/03 19:04

投稿

shinjism
shinjism

スコア11

title CHANGED
File without changes
body CHANGED
@@ -13,4 +13,134 @@
13
13
  XAMPP 5.6.15 (PHP 5.6.15)
14
14
  TCPDF 6.2.12
15
15
  Unity 5.2.3f1
16
- ※Windows PCアプリとXAMPP環境は同一PC内に共存
16
+ ※Windows PCアプリとXAMPP環境は同一PC内に共存
17
+
18
+ 送信側サンプルコード
19
+ ```C#
20
+ private IEnumerator PostWithBinary2(byte[] byteData, System.Action<WWW> callback)
21
+ {
22
+ string url = "http://localhost/report/sample.php";
23
+
24
+ WWWForm form = new WWWForm();
25
+ form.AddField("title", "日本語タイトル");
26
+ form.AddField("date", "2016-01-01");
27
+ form.AddField("copyright", "Copyright (C) 2016 日本語著作者. All Rights Reserved");
28
+ form.AddBinaryData("picture", byteData, "picture.png", "image/png");
29
+
30
+ WWW www = new WWW(url, form);
31
+ yield return www;
32
+
33
+ if (callback != null)
34
+ {
35
+ callback(www);
36
+ }
37
+ }
38
+ ```
39
+ 受信側サンプルコード
40
+ ```PHP
41
+ <?php
42
+ ini_set('memory_limit','128M');
43
+ require_once('./tcpdf/tcpdf.php');
44
+
45
+ // Extend the TCPDF class to create custom Header and Footer
46
+ class MYPDF extends TCPDF
47
+ {
48
+ private $hTitle='';
49
+ private $hDate='';
50
+ private $fCopyright='';
51
+
52
+ //Constructor
53
+ function __construct($orientation,$unit,$format,$unicode,$encoding,$diskcache,$pdfa,$title,$date,$copyright)
54
+ {
55
+ parent::__construct($orientation,$unit,$format,$unicode,$encoding,$diskcache,$pdfa);
56
+ $this->hTitle=$title;
57
+ $this->hDate=$date;
58
+ $this->fCopyright=$copyright;
59
+ }
60
+
61
+ //Page header
62
+ public function Header() {
63
+ // Logo
64
+ $imageFile=dirname(__FILE__).'/logo.jpg';
65
+ if(file_exists($imageFile))
66
+ {
67
+ $this->Image($imageFile,10,15,32,0,'JPG','','T',false,150,'',false,false,0,false,false,false);
68
+ }
69
+ // Title
70
+ $this->SetXY(50,22.5);
71
+ $this->SetFont('cid0jp','B',16);
72
+ $this->Cell(110,20,$this->hTitle,0,false,'C',0,'',0,false,'L','M');
73
+ // Date
74
+ $this->SetX(190);
75
+ $this->SetFont('cid0jp','',14);
76
+ $this->Cell(0,20,$this->hDate,0,false,'R',0,'',0,false,'L','M');
77
+ }
78
+
79
+ // Page footer
80
+ public function Footer() {
81
+ // Copyright
82
+ $this->SetY(-15);
83
+ $this->SetFont('cid0jp','',10);
84
+ $this->Cell(0,10,$this->fCopyright,0,false,'C',0,'',0,false,'T','M');
85
+ }
86
+ }
87
+
88
+ // check Image
89
+ function checkImage($file)
90
+ {
91
+ if($file['type']!='image/png')
92
+ {
93
+ return false;
94
+ }
95
+ if($file['error']>0)
96
+ {
97
+ return false;
98
+ }
99
+ if(!move_uploaded_file($file['tmp_name'],dirname(__FILE__).'/images/'.$file['name']))
100
+ {
101
+ return false;
102
+ }
103
+ return true;
104
+ }
105
+
106
+ // create new PDF document
107
+ $pdf=new MYPDF(PDF_PAGE_ORIENTATION,PDF_UNIT,PDF_PAGE_FORMAT,true,'UTF-8',false,false,$_POST['title'],$_POST['date'],$_POST['copyright']);
108
+ $pdf->setHeaderData(PDF_HEADER_LOGO,PDF_HEADER_LOGO_WIDTH,PDF_HEADER_TITLE,PDF_HEADER_STRING);
109
+ $pdf->setHeaderFont(Array('cid0jp','',14));
110
+ $pdf->AddPage();
111
+ $pdf->SetFont('cid0jp','',14);
112
+ $pdf->SetFillColor(230);
113
+
114
+ // output Image
115
+ if(array_key_exists('picture',$_FILES))
116
+ {
117
+ $file=$_FILES['picture'];
118
+ if (checkImage($file))
119
+ {
120
+ $x=10;
121
+ $y=125;
122
+ $pdf->Image(dirname(__FILE__).'/images/'.$file['name'],$x,$y,92,0,'PNG','','',true,200,'',false,false,1,false,false,false);
123
+ }
124
+ }
125
+
126
+ // save PDF
127
+ $reportFile=dirname(__FILE__).'/report.pdf';
128
+ if(file_exists($reportFile))
129
+ {
130
+ unlink($reportFile);
131
+ }
132
+ $pdf->Output($reportFile,'F');
133
+
134
+ // return JSON
135
+ $json = array(
136
+ 'resultReportFile'=>(empty($_SERVER['HTTPS'])?'http://':'https://').$_SERVER['HTTP_HOST'].(dirname($_SERVER['SCRIPT_NAME']).'/report.pdf'),
137
+ 'error'=>'',
138
+ );
139
+ header('Content-Type: application/json');
140
+ echo json_encode($json);
141
+ ?>
142
+ ```
143
+ 文字化けしなかった例
144
+ ![ヘッダー部とフッター部に日本語テキスト出力。左上の青い箇所はPHPスクリプトと同じ場所にある画像を出力。](4d8f47ae413c70b00db69ad37ee7b2f5.png)
145
+ 文字化けした例
146
+ ![ヘッダー部とフッター部に日本語テキスト出力。中央の画像がpostしたもの。](7ce6d25783a51d4e9ef0ae446d81e9c5.png)

1

postする画像はバイナリデータである旨を追記しました。

2016/02/03 19:04

投稿

shinjism
shinjism

スコア11

title CHANGED
File without changes
body CHANGED
@@ -1,4 +1,4 @@
1
- Unityで作成したWindows PCアプリから、PDF生成を行うPHPスクリプトに対して日本語テキストと100KB未満の画像をpostし、PDF出力を行っています。
1
+ Unityで作成したWindows PCアプリから、PDF生成を行うPHPスクリプトに対して日本語テキストと100KB未満の画像(バイナリデータ)をpostし、PDF出力を行っています。
2
2
 
3
3
  日本語テキストのみpostした場合は、文字化けなく正常に出力されます。
4
4
  日本語テキストと画像1点をpostした場合は、画像は正常に出力されますが、日本語テキストの文字化けが発生します。