Welcome Guest, Not a member yet? Register   Sign In
Pass PDF to User from Web Service
#1

[eluser]bernie145[/eluser]
Hello all,

I'm working on a site that allows students to sell books to us and print shipping labels to ship the books. I have the FedEx API working to accept the user and item data, create a label, and save the label as a pdf file in an application/labels folder. My question is, is there a way to have the controller that talks to the API and gets the label pass it straight to the user? Right now, the user clicks on a "print your label link" which calls the fedex/create_label function, but the only thing I've been able to figure out is to then load another view (print_shipment_view) that has a link to the actual label. This seems like an extra step to me. Shouldn't there be a way to open the pdf that was generated directly from the controller?

Not sure if you need to see any of this or not...

Code:
$response = $client->processShipment($request); // FedEx web service invocation    
            if ($response->HighestSeverity != 'FAILURE' && $response->HighestSeverity != 'ERROR')
            {
                $this->fedex_common->printSuccess($client, $response);
                $ship_label='application/labels/'.$transaction_id.'.pdf';
                $fp = fopen($ship_label, 'wb');  
                fwrite($fp, ($response->CompletedShipmentDetail->CompletedPackageDetails->Label->Parts->Image));
                fclose($fp);
                $this->DB_model->insert_tracking_number($transaction_id,$response->CompletedShipmentDetail->CompletedPackageDetails->TrackingIds->TrackingNumber);

                                $data['buyback_id'] = $transaction_id;
                                $data['tracking_number'] = $response->CompletedShipmentDetail->CompletedPackageDetails->TrackingIds->TrackingNumber;
                                $data['ship_label'] = $ship_label;
                                
                echo $this->load->view('print_shipment_view',$data,true);
            }
#2

[eluser]Aken[/eluser]
Easy way would be to set PHP headers to display/download the PDF file after you've written it. A quick example of that is on the PHP manual page for header(): http://php.net/manual/en/function.header.php

Alternatively, you could use PDF generation code to create a PDF file straight from the $response->...->Image resource instead of using fwrite() and such, then display/download it using the same headers from above.

If you're having a hard time finding solutions based on those recommendations, let me / us know and we'll help some more.
#3

[eluser]bernie145[/eluser]
Aken,

Thank you! That's exactly what I was looking for.

Working Code...
Code:
if ($response->HighestSeverity != 'FAILURE' && $response->HighestSeverity != 'ERROR')
            {
                $this->fedex_common->printSuccess($client, $response);
                $ship_label='application/labels/'.$transaction_id.'.pdf';
                $fp = fopen($ship_label, 'wb');  
                fwrite($fp, ($response->CompletedShipmentDetail->CompletedPackageDetails->Label->Parts->Image));
                fclose($fp);
                $this->DB_model->insert_tracking_number($transaction_id,$response->CompletedShipmentDetail->CompletedPackageDetails->TrackingIds->TrackingNumber);
                                
                //The PDF is created, now we need to send it to the user
                if (file_exists($ship_label)) {
                    header('Content-type: application/pdf');
                    header('Content-Disposition: attachment; filename="shipping label.pdf"');
                    readfile($ship_label);
                    exit;
                } else {
                    echo 'Error: No File at this location';
                }
            }
#4

[eluser]russ_kern[/eluser]
Seems like you are good to go, but as an option, I've been using fPDF to allow a user to create custom sellsheets out of a Code Ignitor App.

It's an old library, but may be worth a look sometime... and it's opensource.

R




Theme © iAndrew 2016 - Forum software by © MyBB