Unityで作成したWindows PCアプリから、PDF生成を行うPHPスクリプトに対して日本語テキストと100KB未満の画像(バイナリデータ)をpostし、PDF出力を行っています。
日本語テキストのみpostした場合は、文字化けなく正常に出力されます。
日本語テキストと画像1点をpostした場合は、画像は正常に出力されますが、日本語テキストの文字化けが発生します。
PHPスクリプトの画像出力箇所をコメントアウトして、日本語テキストと画像1点をpostした場合も、日本語テキストの文字化けが発生します。
メモリ不足が問題ではないかと思い、ini_set関数やphp.iniなどでmemory_limitを128M、256M、512Mと変えてみましたが、結果は変わりませんでした。
もし、上記の現象や原因に関する情報やヒントがございましたら、ご教示よろしくお願い致します。
環境は次のとおりです。
Windows 10 Pro 64bit
XAMPP 5.6.15 (PHP 5.6.15)
TCPDF 6.2.12
Unity 5.2.3f1
※Windows PCアプリとXAMPP環境は同一PC内に共存
送信側サンプルコード
C#
1private IEnumerator PostWithBinary2(byte[] byteData, System.Action<WWW> callback) 2{ 3 string url = "http://localhost/report/sample.php"; 4 5 WWWForm form = new WWWForm(); 6 form.AddField("title", "日本語タイトル"); 7 form.AddField("date", "2016-01-01"); 8 form.AddField("copyright", "Copyright (C) 2016 日本語著作者. All Rights Reserved"); 9 form.AddBinaryData("picture", byteData, "picture.png", "image/png"); 10 11 WWW www = new WWW(url, form); 12 yield return www; 13 14 if (callback != null) 15 { 16 callback(www); 17 } 18}
受信側サンプルコード
PHP
1<?php 2ini_set('memory_limit','128M'); 3require_once('./tcpdf/tcpdf.php'); 4 5// Extend the TCPDF class to create custom Header and Footer 6class MYPDF extends TCPDF 7{ 8 private $hTitle=''; 9 private $hDate=''; 10 private $fCopyright=''; 11 12 //Constructor 13 function __construct($orientation,$unit,$format,$unicode,$encoding,$diskcache,$pdfa,$title,$date,$copyright) 14 { 15 parent::__construct($orientation,$unit,$format,$unicode,$encoding,$diskcache,$pdfa); 16 $this->hTitle=$title; 17 $this->hDate=$date; 18 $this->fCopyright=$copyright; 19 } 20 21 //Page header 22 public function Header() { 23 // Logo 24 $imageFile=dirname(__FILE__).'/logo.jpg'; 25 if(file_exists($imageFile)) 26 { 27 $this->Image($imageFile,10,15,32,0,'JPG','','T',false,150,'',false,false,0,false,false,false); 28 } 29 // Title 30 $this->SetXY(50,22.5); 31 $this->SetFont('cid0jp','B',16); 32 $this->Cell(110,20,$this->hTitle,0,false,'C',0,'',0,false,'L','M'); 33 // Date 34 $this->SetX(190); 35 $this->SetFont('cid0jp','',14); 36 $this->Cell(0,20,$this->hDate,0,false,'R',0,'',0,false,'L','M'); 37 } 38 39 // Page footer 40 public function Footer() { 41 // Copyright 42 $this->SetY(-15); 43 $this->SetFont('cid0jp','',10); 44 $this->Cell(0,10,$this->fCopyright,0,false,'C',0,'',0,false,'T','M'); 45 } 46} 47 48// check Image 49function checkImage($file) 50{ 51 if($file['type']!='image/png') 52 { 53 return false; 54 } 55 if($file['error']>0) 56 { 57 return false; 58 } 59 if(!move_uploaded_file($file['tmp_name'],dirname(__FILE__).'/images/'.$file['name'])) 60 { 61 return false; 62 } 63 return true; 64} 65 66// create new PDF document 67$pdf=new MYPDF(PDF_PAGE_ORIENTATION,PDF_UNIT,PDF_PAGE_FORMAT,true,'UTF-8',false,false,$_POST['title'],$_POST['date'],$_POST['copyright']); 68$pdf->setHeaderData(PDF_HEADER_LOGO,PDF_HEADER_LOGO_WIDTH,PDF_HEADER_TITLE,PDF_HEADER_STRING); 69$pdf->setHeaderFont(Array('cid0jp','',14)); 70$pdf->AddPage(); 71$pdf->SetFont('cid0jp','',14); 72$pdf->SetFillColor(230); 73 74// output Image 75if(array_key_exists('picture',$_FILES)) 76{ 77 $file=$_FILES['picture']; 78 if (checkImage($file)) 79 { 80 $x=10; 81 $y=125; 82 $pdf->Image(dirname(__FILE__).'/images/'.$file['name'],$x,$y,92,0,'PNG','','',true,200,'',false,false,1,false,false,false); 83 } 84} 85 86// save PDF 87$reportFile=dirname(__FILE__).'/report.pdf'; 88if(file_exists($reportFile)) 89{ 90 unlink($reportFile); 91} 92$pdf->Output($reportFile,'F'); 93 94// return JSON 95$json = array( 96 'resultReportFile'=>(empty($_SERVER['HTTPS'])?'http://':'https://').$_SERVER['HTTP_HOST'].(dirname($_SERVER['SCRIPT_NAME']).'/report.pdf'), 97 'error'=>'', 98); 99header('Content-Type: application/json'); 100echo json_encode($json); 101?>
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。