简体   繁体   中英

Retrieving PDF file with cURL and an invoice generator API

For my last university project of this semester, I am trying to generate an invoice with an API found on the web, calling it with cURL which I think it's the most simple option.

Unfortunately, when I execute the code I do not receive a readable page or a pdf file and I have no idea how to proceed from here.

Any help would be very much appreciated!

      <?php



    $invoice = array(
        "logo" => "test.dev", // url to logo
        "from" => "Wouter van Marrum \n<a href=\"http://www.concept-core.nl\">concept-core.nl</a>", // The name of your organization
        "to" => "marleen", // The entity being billed - multiple lines ok
        "number" => 20001, // Invoice number
        //"purchase_order" => "", // Purchase order number
        "date" => null, // Invoice date
        "due_date" => null, // Invoice due date
        "payment_terms" => null, // Payment terms summary (i.e. NET 30)
        // items
        "items[0][name]" => "Website",
        "items[0][quantity]" => 1,
        "items[0][unit_cost]" => 300,
        "items[1][name]" => "logo",
        "items[1][quantity]" => 1,
        "items[1][unit_cost]" => 150,
        // End items
        // Fields
        "fields[tax]" => true, // can be true, false
        "fields[discounts]" => false, // Subtotal discounts - numbers only
        "fields[shipping]" => false, // Can be true or false
        // End fields
        "tax"=> 21, // Tax - numbers only
        "amount_paid" => 0, // Amount paid - numbers only
        "notes" => "", // Notes - any extra information not included elsewhere
        "terms" => "Lees dit", // Terms and conditions - all the details
    );

    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, "https://invoice-generator.com");
    curl_setopt($ch, CURLOPT_POST, 1);
    curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_POSTFIELDS, $invoice);


    $result = curl_exec($ch);

  $ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "https://invoice-generator.com");
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $invoice);


    $result = curl_exec($ch);

    header('Content-Description: File Transfer');
    header('Content-type: application/pdf');
    header('Content-Disposition: attachment; filename="invoice_'.$date->toDateString().'.pdf"');
    header('Expires: 0');
    header('Cache-Control: must-revalidate');
    header('Pragma: public');
    header('Content-Length: '.strlen($result));




    curl_close($ch);



    echo $result;

I have found the documentation for this API here ( https://github.com/wotta/Invoiced-backup/blob/master/app/ConceptCore/Invoiced/Core.php ) but I can't understand which additional steps I am missing.

Just to give you an idea, this is part of what I see when executing the code:

%PDF-1.4 1 0 obj << /Title ( Invoice) /Creator ( wkhtmltopdf 0.12.2.1) /Producer ( Qt 4.8.6) /CreationDate (D:20191206214540Z) >> endobj 3 0 obj << /Type /ExtGState /SA true /SM 0.02 /ca 1.0 /CA 1.0 /AIS false /SMask /None>> endobj 4 0 obj [/Pattern /DeviceRGB] endobj 8 0 obj << /Type /Catalog /Pages 2 0 R >> endobj 5 0 obj << /Type /Page /Parent 2 0 R /Contents 9 0 R /Resources 11 0 R /Annots 12 0 R /MediaBox [0 0 612 792] >> endobj 11 0 obj << /ColorSpace << /PCSp 4 0 R /CSp /DeviceRGB /CSpg /DeviceGray >> /ExtGState << /GSa 3 0 R >> /Pattern << >> /Font << /F6 6 0 R /F7 7 0 R >> /XObject << >> >> endobj 12 0 obj [ ] endobj 9 0 obj << /Length 10 0 R /Filter /FlateDecode >> stream x \\Ko 6 W @ @Q Σ@ a C mZ, E = "i Ɏ7 Rn Ҍ y}C): ?= N J. & y 1 K? ( \\Q n :/ y |鸉 Gx`+&u u L t p K s L> >> > ;o AW vS r A l K . ( 26 BX w^ Kf PD2 ϝ6R W 7] h)Ӂ^ S҅u Kk ]h;a2P j*ױ?E ]Ig g K ˱ B '; ň U ?tO!^ (P @ }75< U 9+LE ʊ .E uW Glj R R )a:m & U J | b ӳ~ ] "-y|"Ʉ d P <~"? H?w L x G> 4: Ҝ[{ ; ks̹ _ yìS2 Kwh 7 K rq A GA t^s C l + Jkj a,nz ~ {) )I) R/ ~#u] 3 7 fi NWt -%V A wA T R i O ZN ԰q B eRY[+{ 4 Oqx B YeM c @ \\eF^ P 43m q M^ L ѐc ye0e Dd9 L[]L WA< =& AE ׵ F a o \\ 9 NڛJ}1 '-GUp 2x) 1S 6 ݤ dѦό3 7} ; h o ? eK K W 3 ;ć^C4ZU Ait ee zM;Q)

You need add curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1); to specify that CURL return the result as a string and not be formatted. In this case, with this code, the result will be a readable PDF file: screenshot of resultant pdf

In addition, in this case you must replace header('Content-Length: ' . $response->getHeader('Content-Length')[0]); to header('Content-Length: '.strlen($result)); And because of the CURLOPT_RETURNTRANSFER flag, you will not use $file = $result-> getBody(); , try deleting it.

I attached a link with the modified code, currently working on my server: https://pastebin.com/JDgKjUt8

I recommend that you enable error logging to verify what went wrong: Showing all errors and warnings

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM