2016-10-28 9 views
1

내 Laravel 응용 프로그램에서 PHPOffice/PHPWord를 사용하고 있습니다. 테이블에 결과가있는 .docx 문서를 생성하는 데 사용됩니다. 이 6 행 3 개 테이블의 문서에 대해 잘 작동하지만,이 문서가 생성됩니다 더 많은 행이 있지만 그것을 다음과 같은 오류를 열 때하는 것이 발생했을 때 :XML 구문 분석 오류입니다. PHPWord

We're sorry, We can't open (documentname) because we found a problem with its contents. 

Details: XML parsing error Location: Part:/word/document.xml, Line: 2, Column 14349. 

을 지금, 나는 또 다른 결과 페이지 위치에 작업을 시작했습니다 .docx 문서를 생성하고 싶습니다. 여기에는 5 개의 표가 포함되지만 3 행으로 동일한 XML 구문 분석 오류가 있지만 다른 위치 (위치 : 부분 : /word/document.xml, 줄 : 4, 열 : 2888)에 있습니다. 누군가 내 코드 또는 phpword/words에 오류가 있는지 설명 할 수 있습니까?

나는 모든 것을 삭제하고 새로운 행을 천천히 추가함으로써 몇 가지 문제 해결을했습니다. 오류를 발견했지만 어떻게 해결할 수 있습니까? 처음 두 테이블은 훌륭하게 생성됩니다.

$phpWord = new \PhpOffice\PhpWord\PhpWord(); 

    $section = $phpWord->addSection(); 
    $section->addImage('../public/img/2.jpg', array('width' => 230, 'height' => 65, 'alignment' => 'left')); 
    $section->addText('Project IDs:' . $parameter); 
    $header =$section->addHeader(); 
    $header->addText('Results Summary'); 

    $section->addLine(
     array(
      'width'  => \PhpOffice\PhpWord\Shared\Converter::cmToPixel(16), 
      'height'  => \PhpOffice\PhpWord\Shared\Converter::cmToPixel(0), 
      'positioning' => 'absolute', 
     ) 
    ); 
    $tableName = 'rStyle'; 
    $phpWord->addFontStyle($tableName, array('italic' => true, 'size' => 12)); 
    $thName = 'tStyle'; 
    $phpWord->addFontStyle($thName, array('bold' => true, 'size' => 9)); 

    $section->addText('General Information Table', $tableName); 
    $fancyTableStyle = array('borderSize' => 6, 'borderColor' => '999999'); 
    $spanTableStyleName = 'Overview tables'; 
    $phpWord->addTableStyle($spanTableStyleName, $fancyTableStyle); 
    $table = $section->addTable($spanTableStyleName); 
    $table->addRow(null, array('tblHeader' => true, 'cantSplit' => true)); 
    $table->addCell(1750)->addText('Project ID',$thName); 
    $table->addCell(1750)->addText('Description',$thName); 
    $table->addCell(1750)->addText('Notes',$thName); 
    foreach ($id_array_explode as $char) { 
     $table->addRow(); 
     $singlenumber = (int)$char; 
     $cursor = $collection->find(array("id" => $singlenumber)); 
     foreach ($cursor as $document) { 
        $table->addCell(1750)->addText($document["project_id"]); 
        $table->addCell(1750)->addText($document["description"]); 
        $table->addCell(1750)->addText($document["notes"]); 
     } 
    } 
    $section->addText('   
'); 
    $section->addLine(
     array(
      'width'  => \PhpOffice\PhpWord\Shared\Converter::cmToPixel(16), 
      'height'  => \PhpOffice\PhpWord\Shared\Converter::cmToPixel(0), 
      'positioning' => 'absolute', 
     ) 
    ); 
    $section->addText('Input Table', $tableName); 
    $table1 = $section->addTable($spanTableStyleName); 
    $table1->addRow(null, array('tblHeader' => true, 'cantSplit' => true)); 
    $table1->addCell(1750)->addText('Project ID',$thName); 
    $table1->addCell(1750)->addText('#',$thName); 
    foreach ($id_array_explode as $char) { 
     $table1->addRow(); 
     $singlenumber = (int)$char; 
     $cursor = $collection->find(array("id" => $singlenumber)); 
     foreach ($cursor as $document) { 
      if (is_array($document['input'])) { 
       foreach ($document['input'] as $samples) { 
        $table1->addCell(1750)->addText($document["project_id"]); 
        $table1->addCell(1750)->addText($samples['nr']); 
       } 
      } 
     } 
    } 
    $section->addText('   
'); 
    $section->addLine(
     array(
      'width'  => \PhpOffice\PhpWord\Shared\Converter::cmToPixel(16), 
      'height'  => \PhpOffice\PhpWord\Shared\Converter::cmToPixel(0), 
      'positioning' => 'absolute', 
     ) 
    ); 
    $section->addText('Output Table', $tableName); 
    $table2 = $section->addTable($spanTableStyleName); 
//// THIS IS WHERE THE ERROR OCCURS!! 
     $table2->addRow(null, array('tblHeader' => true, 'cantSplit' => true)); 
     $table2->addCell(1750)->addText('ID',$thName); 

고마워요!

해결 방법 오크, 그래서 전체 문서를 삭제하고 오류가 발생한 곳을보기 위해 모든 문장을 따로 추가했습니다. 이로 인해 오류가 내가 얻은 데이터에서 비롯된 것으로 나타났습니다. ">"및 "&"표지판을 처리 할 수 ​​없습니다.

모든 오류가 발생하면 인쇄중인 데이터를 확인하십시오!

+0

이상한 점은 .. -> addRow()에서 매개 변수를 제거하고 한 번에 한 번씩 all -> addCell을 추가하고 다시 작동하는 방식입니다. 문제는 테이블 머리가 너무 많았던 것입니까? ... –

답변

0

이 오류는 XML 파일에 문제가 있음을 나타내며 교차 태그가 있는지, 태그가 열려 있는지, 올바르게 닫히지 않은지 또는 다른 추가 콘텐츠가 추가되었는지 나타냅니다.

세부 사항 : XML 파싱 오류 위치 : 부 : /word/document.xml, 라인 : 2, 열 14349.

XML 파일에 문제가있을 때이 오류가 항상 발생합니다. XML을 작성하거나 XML을 읽는 중입니다.

+0

이 대답은 도움이되지 않습니다. OP가 수동으로 XML 태그를 생성하지 않습니다. 태그는 어떻게 그렇게 되었습니까? 그들이 어떻게 고쳐야합니까? – Simba

+1

세부 정보 : XML 구문 분석 오류 위치 : 파트 : /word/document.xml, 줄 : 2, 열 14349 이 오류는 XML 파일에 문제가있을 때마다 발생합니다. XML을 작성하거나 XML을 읽는 중입니다. –

0

PHPWord에 익숙하지 않지만 문서의 인코딩과 삽입하려는 데이터가 동일한 지 확인하십시오. 엑셀 파일을 생성하기 위해 오래된 라이브러리에서 동일한 문제가 발생했습니다.

3

실제로 XML은 데이터에 포함되어 있습니다. XML 특수 문자가 있고 Word에서 문서를 구문 분석 할 때 이해할 수 없습니다. htmlspecialchars()을 사용하여이 문제를 해결했습니다. 나는 그것이 최선의 방법이라고 확신하지는 않지만 작동한다.

+1

'&'문자가 저를위한 범인이었습니다; phpword는 그 자체로 도망가는 것을 많이 보이지 않습니다. – scurrie