1

I want to get the pdf invoice as response using magento rest api for mobile application. I got the invoice order history data

My code:

public function printInvoicePdf(Request $request)
  {
    echo "testing";

    $validator = Validator::make($request->all(), [ 
        'invoice_id'  => 'required'
        //'token' => 'required'
    ]);
    if($validator->fails()) 
    { 
      return response()->json(['error'=>$validator->errors()], 401);            
    }

    else 
    {

    $invoice_id=$request->invoice_id;
    //print_r($order_id);
    $get_admin_token=$this->authenticateMageAPI();
    //$apiURL=$this->API_URL."invoices/".$invoice_id."/pdf/".templateId."";
    $apiURL=$this->API_URL."invoices/".$invoice_id; 

    //echo 
    $headers = array("Authorization: Bearer ".$get_admin_token,'Content-Type: application/pdf');

    $ch = curl_init($apiURL);
    curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "GET");
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);

    // curl_setopt($ch, CURLOPT_HTTPHEADER, array(
    //     "Authorization: Bearer ".$token
    // ));
    $result = curl_exec($ch);
    $response_get = json_decode($result);
    if(isset($response_get->message))
      {
        echo "testing3";
        $respose    = array("error_code" => 1,"data"=>$response_get); 
        $data_enode = json_encode($respose);
        echo  $data_enode;  
      }
      else
        {

          echo "testing2";
            $respose    = array("error_code" => 0,"data"=>$response_get); 
            $data_enode = json_encode($respose);
            echo  $data_enode;  
        }    
    }
  }

Any help would be appreciated

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Browse other questions tagged or ask your own question.