0

I'm generating a PDF file using the PdfLib PHP extension. I start a new file, load an image into it, and call end_document to finish the file.

All is working fine, the file is generated correctly, but I'm trying to upload the file to the Amazon S3 cloud immediately (in the same function that generates the file) after generating it. The file is uploaded but it is not complete, seems like an empty pdf is uploaded.

// get a PDFLib object
$p = new PDFlib();

// determine filename
$pageFileName = $page->getPageNr() . '_' . sha1(microtime()) . '.pdf';

// determine local file path and S3 file path
$filePath = $this->fs->getPath(
    Dt_Util_FileStorage_Path::FILETYPE_SOURCEFILE,
    $pageFileName,
    $job
);

// start the document
if ($p->begin_document($filePath->getTmpPath(), "") == 0) {
    throw new Dt_Exception_DtException($p->get_errmsg(), 3, null);
}

// begin a new page inside the new document
$p->begin_page_ext($widthScreenResPixels, $heightScreenResPixels, "");

// load an image resource
$image = $p->load_image("auto", $imgfile, "");
if ($image == -1){
    throw new Exception("Error: " + $p->get_errmsg());
}

// load the image into the current page
$p->fit_image($image, 0, 0, "boxsize={" . $widthScreenResPixels . " " . $heightScreenResPixels . "} fitmethod=entire");

// end the page
$p->end_page_ext("");

// release the image
$p->close_image($image);

//write PDF file
$p->end_document("");

// sleep(5);

// upload the file to S3
$this->fs->putFile($filePath);

The location where I keep the file locally contains the file as it should be: the image is loaded into it and the file is about 600KB in size. The uploaded file is like an empty skeleton, the file that was generated when I called PdfLib->begin_document but before anything is actually placed inside it, I can't open it and it is 104 Bytes in size.

I tried sleeping for several seconds after end_document and before the upload, because I thought maybe PdfLib needed some time to finish things up, but this has no effect, the results are exactly the same.

Does anyone have any ideas on how to fix this, what can cause this, where to look for a solution?

UPDATE The problem seems to be the S3 api class I'm using. I was using this one. Using the official SDK the upload succeeds as expected. I will continue using the official SDK. The original question, why it failed using the other API class, remains.

Asciiom
  • 9,867
  • 7
  • 38
  • 57
  • would you mind sharing the upload script (sans API docs). It would be helpful to see how you are sending the temporary file up to S3 – JM4 Jul 17 '13 at 00:31
  • I wrote that for my previous employer, I don't have access to the code anymore. It was pretty basic usage of the API though, nothing too fancy! – Asciiom Jul 17 '13 at 09:53

0 Answers0