I want to create an XML file based on a pdf. I have the following set up:
<?php
$pdf = 'http://ift.tt/1KO0DEk';
/**
* 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://ift.tt/1KO0DEm;
<ProviderURL>http://ift.tt/1AUWkRY;
<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);
}
I was wondering if I am going about this correctly or if there is a better way to do it. Basically, I want the script to see if the XML file associated with the pdf exists, if it does not; I want it to create the xml file.
No comments:
Post a Comment