0

Wonder if anyone can explain what rstrictions Xero has for adding a pdf file I creted using mPDF. I have managed to use a png file and uploaded it to an invoice but cannot get past this block.

public function attachPDF( $invoice ) {
        try {
        $company = Company::find( $invoice->company_id );
        list( $xeroTenantId, $accountingApi, $identityApi ) = VivattiXero::getApiConnection( $company, false );
        $filename = 'AUTH#' . sprintf( '%08d', $invoice->id ) . '.pdf';
        //$filename = 'favicon.png';
        $guid     = $invoice->invoice_id;
        $contents = $invoice->getPDF( \Mpdf\Output\Destination::FILE, true, $filename );
        $handle = fopen( $filename, "r" );
        $contents = fread( $handle, filesize( $filename ) );
        fclose( $handle );
        //unlink( $filename );
        $result['apiResponse'] = $accountingApi->createInvoiceAttachmentByFileName( $xeroTenantId, $guid, $filename, $contents, true );
        $result['message'] = 'PDF was attached to invoice #' .  sprintf( '%08d', $invoice->id );
        $result['success'] = true;
        }catch (\XeroAPI\XeroPHP\ApiException $e) {
            $error = AccountingObjectSerializer::deserialize(
                $e->getResponseBody(),
                '\XeroAPI\XeroPHP\Models\Accounting\Error',
                []
            );
            $result['message'] = "ApiException - " . $error->getElements()[0]["validation_errors"][0]["message"];
            $result['success'] = false;
        }
        return $result;
    }
}

Has anyone successfully uploaded a pdf to xero? anyone with a mpdf pdf?

thanks

ThurstonLevi
  • 664
  • 13
  • 34
  • Do you get an error message, or does it just silently fail? – droopsnoot Jun 04 '20 at 08:38
  • 1
    It may be me, but if you look in the source for the PHP SDK at https://raw.githubusercontent.com/XeroAPI/xero-php-oauth2/master/lib/Api/AccountingApi.php and in particular the createInvoiceAttachmentByFileNameRequest function definition, I can't see anywhere that it sets the Content-Type header to denote a PDF file. I don't know if this is contributing to the issue, the doc suggests that it needs to be set appropriately, but the source suggests it's always either `application/json` or `application/octet-stream`. – droopsnoot Jun 04 '20 at 08:57
  • @droopsnoot I did get an error 'ApiException - The file couldn't be uploaded because it isn't a supported file type.' My initial attempt at this to build it as an attachment in Invoice (https://stackoverflow.com/questions/62155858/add-an-attachment-in-xero-using-php-xero-api). I think you may be onto something with this as I am able to upload these files within xero. mPdf uses PDF1.4 and was thinking maybe I need to use something else to render the pdf but would rather not do that refactoring... – ThurstonLevi Jun 04 '20 at 09:27
  • I was also able to add a png with this method but that would work under octet-stream right? – ThurstonLevi Jun 04 '20 at 09:29
  • It's not something I've tried, also I'm not using PHP, or any API for that matter - I found the c# so confusing I do direct HTTP Post and Get and bypass it all. It just struck me that the docs show the Content-type as image/png in their example, but the SDK doesn't seem to set that. That said, the doc says to _not_ JSON-encode it, but the SDK seems to be doing. – droopsnoot Jun 04 '20 at 09:56
  • @droopsnoot I'm beginning to think the same way, The time I've spent on this beggars belief! I'm thinking directly calling this with curl would make sense and should be simpler. Would you have an example json/xml of what i should be submitting at all? – ThurstonLevi Jun 04 '20 at 11:48
  • Not really, but there's a video ( https://www.youtube.com/watch?v=Zcf_64yreVI ) that shows how to refresh tokens and so on, but towards the end does show what needs to be in headers and content for various comms with Xero and it's quite easy to figure out from there. I use all the Xero-supplied models in VB to get the content structures correct, then just JSON-encode them prior to calling POST or PUT. – droopsnoot Jun 04 '20 at 11:53

0 Answers0