从PDF创建XML

时间:2015-01-15 18:47:54

标签: php xml

我想基于pdf创建一个XML文件。我有以下设置:

<?php
$pdf = 'http://mywebsite.com/files/test.pdf';

/**
 * Get Remote File Size
 *
 * @param sting $url as remote file URL
 * @return int as file size in byte
 */
function remote_file_size($url){
    # Get all header information
    $data = get_headers($url, true);
    # Look up validity
    if (isset($data['Content-Length']))
        # Return file size
        return (int) $data['Content-Length'];
}

function createxml($pdf){
    $file = pathinfo($pdf);
    $file_dir = $file['dirname'];
    $file_base = $file['basename'];
    $file_ext = $file['extension'];
    $file_name = $file['filename'];
    $file_size = remote_file_size($pdf);

    $date = date('Y-m-d H:i:s');

$xml = <<<EOT
<?xml version="1.0" encoding="utf-8"?> 
<BluebeamRemotePackage Version="1.1" ID="pk123"> 
 <Provider>ProviderKey</Provider> 
 <LogoURL>http://mywebsite.com/files/logo.png</LogoURL> 
 <ProviderURL>www.mywebsite.com</ProviderURL> 
 <UserID>$user_id</UserID> 
 <UserName>$user_displayname</UserName> 
 <CompanyName>My Company</CompanyName> 
 <UserType>$user_company</UserType> 
 <Date>$date</Date> 
 <Project Name="$project" ID="$post_id"> 
  <Company>$user_company</Company> 
  <ProjectURL>$post_url</ProjectURL> 
  <File Name="$file_name" ID="$file_base"> 
   <FileURL>$pdf</FileURL> 
   <Size>$file_size</Size>  
   <Date>$date</Date> 
   <Load>True</Load>  
   <Security> 
    … 
   </Security> 
 </Project> 
</BluebeamRemotePackage> 
EOT;


    $dom = new DomDocument;
    $dom->preserveWhiteSpace = FALSE;
    $dom->loadXML($xml);

    echo $dom->saveXML();
}

if (file_exists($pdf)) {
  $xml = $pdf['filename'] . '.xml';
    header('Content-Type: application/octet-stream');
    header("Content-Transfer-Encoding: Binary"); 
    header("Content-disposition: attachment; filename=\"" . basename($xml) . "\""); 
    readfile($xml); // do the double-download-dance (dirty but worky)
} else {
  createxml($pdf);
}

我想知道我是否正确地解决了这个问题,或者是否有更好的方法来做到这一点。基本上,我希望脚本查看与pdf关联的XML文件是否存在,如果不存在;我想要它来创建xml文件。

0 个答案:

没有答案