Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to convert Pdf file to byte array in php and send?

I have found pdf to byte array and vice-versa in java,dotnet and python. But i want to convert pdf to byte array in php laravel. I am using "IMUIS" which is accounting software solution and need to sending journal entries from laravel lumen to "IMUIS" for processing.But it gives the error after converting.

"Foutmelding": "Kan een object van het type System.String niet converteren naar het type System.Byte[]."

In english that means

"Error message": "Can not convert a System.String object to the System.Byte [] type."

The documentation is given here:

doc link

Here is the code for it.

public function saveJournal($values = '') {
    //echo "adasd";dd();
    $partnerKey = $values->input('Partnerkey');
    $omgevingscode = $values->input('Environmentcode');  
    $file = file_get_contents($values->file('Pdffile'));
    $str = base64_encode($file);
    $options = array(
        \WsdlToPhp\PackageBase\AbstractSoapClientBase::WSDL_URL => env('IMUIS_URL'),
        \WsdlToPhp\PackageBase\AbstractSoapClientBase::WSDL_CLASSMAP => ClassMap::get(),
    );
    $login = new \mysdk\ImuisSDK\ServiceType\Login($options);
    if ($login->Login(new \mysdk\ImuisSDK\StructType\Login($partnerKey, $omgevingscode)) !== false) {
        $sessionid = $login->getResult()->SessionId;
    }

    $array = [
        'BOE' => [
           'JR' => '2018',
           'PN' => '5',
           'DAGB' => 20,
           'REK' => 20032,
           'TEGREK' => '40',
           'FACT' => 0,
           'BTW' => 4,
           'BEDRBOEK' => 123.45,
           'DAT' => '08-05-2018',
           'OPM' => 'Anand testing from wsdl',
           'BEDRBTW' => 21,
           'FACT' => 0,
           'OMSCHR' => 'Testing from wsdl api',
           'BOEKSTUK' => 2018075
        ],
        'DIGDOS' => [
            'FILE' => $str
        ]
    ];

    $journaalpost = ArrayToXml::convert($array, 'NewDataSet');//convert array to xml string

    $create = new \mysdk\ImuisSDK\ServiceType\Create($options);
    if ($create->CreateJournaalpost(new \mysdk\ImuisSDK\StructType\CreateJournaalpost($partnerKey, $omgevingscode, $sessionid, $journaalpost)) !== false) {
        $jsonResponse = $create->getResult();
    } else {
        $jsonResponse = $create->getLastError();
    }
    return $jsonResponse;
}

and here is the response as well:

{
    "success": true,
    "result": {
        "CreateJournaalpostResult": false,
        "Journaalpost": "<?xml version=\"1.0\"?>\n<NewDataSet><BOE><JR>2018</JR><PN>5</PN><DAGB>20</DAGB><REK>20032</REK><TEGREK>40</TEGREK><FACT>0</FACT><BTW>4</BTW><BEDRBOEK>123.45</BEDRBOEK><DAT>08-05-2018</DAT><OPM>Anand testing from wsdl</OPM><BEDRBTW>21</BEDRBTW><OMSCHR>Testing from wsdl api</OMSCHR><BOEKSTUK>2018075</BOEKSTUK></BOE><DIGDOS><FILE>JVBERi0xLjMKMyAwIG9iago8PC9UeXBlIC9QYWdlCi9QYXJlbnQgMSAwIFIKL1Jlc291cmNlcyAyIDAgUgovQ29udGVudHMgNCAwIFI+PgplbmRvYmoKNCAwIG9iago8PC9GaWx0ZXIgL0ZsYXRlRGVjb2RlIC9MZW5ndGggMzg2Pj4Kc3RyZWFtCniclZNPb9pAEMXvfIp3TA8Zdme93jE3G0xFJWgKJtdolbhICTYU6L9v3zWEGKnUVeST1++9+b2xzfjUU2QdfvayAv2xRkJKofiKvOh9g00ssUCFS0xMzFDNY0uxw2OF/kRjtMGXxms06QSOLUnwP+Fmkd+lyMr9wddP+9r7aoDxfFE8NOfaKqtZO+PoV7X+gOK5mdekSBhg/opJfb0qK+9fDthsB2Cl5VbZWx1D80DZAbtzhiIX2iiSJGBTIoLd6mo1hRXesGMRSuxx3ixNpxjl2aRY5vPX1NDwzcASk1GXjnE6LJbL+Ww5nV51RCykokvHZDZMF5+R5aN5+rF1BJ2xcDoiY1DBSmBT5/s1FqceDPl3D8fEpxmj8schLB/DTbX19e+OJq1Hc+Q6CrTCJBzwFeUrhQ1fTfROitYTs+mAaHVamU4KI8TmnRStJ+Zr0WeKVvefVbBu/prjS6/333e+fiwx9bVflbs9MrqnDpjWq7WRDpoLYSTOUXyxwD9RUucvCmVuZHN0cmVhbQplbmRvYmoKMSAwIG9iago8PC9UeXBlIC9QYWdlcwovS2lkcyBbMyAwIFIgXQovQ291bnQgMQovTWVkaWV5s7I11RFTyPO/t9OL74tl5/das6enN0bXwr//AKZ629kqeo76x9P/AOOs4y+N/wDK6nJuq/7nerWTI6zQrTReVjEVir+fVST/AF1m6Wrf7W+uMQ+o/D744dNdW2I8fKx2Ny8nCOrKu5j18zJOGq/JoeLX9ndp47YfR9t7+zUmmy59IPG9wAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAABuIAowMDAwMDc2NDgzIDAwMDAwIG4gCjAwMDAwNzY1NTkgMDAwMDAgbiAKdHJhaWxlcgo8PAovU2l6ZSAxMQovUm9vdCAxMCAwIFIKL0luZm8gOSAwIFIKPj4Kc3RhcnR4cmVmCjc2NjA5CiUlRU9GCg0KCiAgICAgIA==</FILE></DIGDOS></NewDataSet>\n",
        "Primarykey": null,
        "Foutmelding": "Kan een object van het type System.String niet converteren naar het type System.Byte[]."
    }
}
like image 373
Anand Pandey Avatar asked May 22 '18 06:05

Anand Pandey


People also ask

How do I convert a PDF to a byte?

Convert PDF File to Byte Array using C#Load input PDF File. Initialize a Byte Array. Initialize FileStream object. Load the file contents in the byte array.

How do I read a PDF in byte array?

You can read data from a PDF file using the read() method of the FileInputStream class this method requires a byte array as a parameter.

How do I convert a PDF document to a preview image in PHP?

php $im = new Imagick(); $im->setResolution(300, 300); //set the resolution of the resulting jpg $im->readImage('file. pdf[0]'); //[0] for the first page $im->setImageFormat('jpg'); header('Content-Type: image/jpeg'); echo $im; ?>

How to convert PDF to a byte array?

You need to follow the steps below for converting PDF to a byte array: 1 Load input PDF File 2 Initialize a Byte Array 3 Initialize FileStream object 4 Load the file contents in the byte array

How to convert a file to ByteArray in ASP NET?

1 Create an ASP.Net application and add a class Document. ... 2 Create a file of format doc/pdf/rtf etc. and convert the file content to a ByteArray using the following method. ... 3 Now we need to convert the byte array back again to the file content and display it to the user with download options. ... More items...

How to generate a PDF file in PHP using FPDF?

You need to download the FPDF class from the FPDF website and include it in your PHP script. Instantiate and use the FPDF class according to your need as shown in the following examples. Example 1: The following example generates a PDF file with the given text in the code.

How to read data from a PDF file in Java?

You can read data from a PDF file using the read () method of the FileInputStream class this method requires a byte array as a parameter.


1 Answers

To convert PDF to byte array you will have to read the document using file_get_contents() and then parse it by function unpack().

<?php
    public function saveJournal($values = '') {
        $partnerKey = $values->input('Partnerkey');
        $omgevingscode = $values->input('Environmentcode');  
        $file = file_get_contents($values->file('Pdffile'));
        $byte_array = unpack("C*",$file);
        $base64_encode = base64_encode(serialize($byte_array));
        $options = array(
            \WsdlToPhp\PackageBase\AbstractSoapClientBase::WSDL_URL => env('IMUIS_URL'),
            \WsdlToPhp\PackageBase\AbstractSoapClientBase::WSDL_CLASSMAP => ClassMap::get(),
        );
        $login = new \mysdk\ImuisSDK\ServiceType\Login($options);
        if ($login->Login(new \mysdk\ImuisSDK\StructType\Login($partnerKey, $omgevingscode)) !== false) {
            $sessionid = $login->getResult()->SessionId;
        }

        $array = [
            'BOE' => [
               'JR' => '2018',
               'PN' => '5',
               'DAGB' => 20,
               'REK' => 20032,
               'TEGREK' => '40',
               'FACT' => 0,
               'BTW' => 4,
               'BEDRBOEK' => 123.45,
               'DAT' => '08-05-2018',
               'OPM' => 'Anand testing from wsdl',
               'BEDRBTW' => 21,
               'FACT' => 0,
               'OMSCHR' => 'Testing from wsdl api',
               'BOEKSTUK' => 2018075
            ],
            'DIGDOS' => [
                'FILE' => $base64_encode 
            ]
        ];

        $journaalpost = ArrayToXml::convert($array, 'NewDataSet');//convert array to xml string

        $create = new \mysdk\ImuisSDK\ServiceType\Create($options);
        if ($create->CreateJournaalpost(new \mysdk\ImuisSDK\StructType\CreateJournaalpost($partnerKey, $omgevingscode, $sessionid, $journaalpost)) !== false) {
            $jsonResponse = $create->getResult();
        } else {
            $jsonResponse = $create->getLastError();
        }
        return $jsonResponse;
    }
?>
like image 193
Zafahix Avatar answered Sep 19 '22 14:09

Zafahix