简体   繁体   English

如何将Pdf文件转换为php中的字节数组并发送?

[英]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. 我发现pdf到字节数组,反之亦然,在java,dotnet和python中。 But i want to convert pdf to byte array in php laravel. 但是我想将pdf转换为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. 我使用的是会计软件解决方案“ IMUIS”,需要将日志条目从laravel流明发送到“ IMUIS”进行处理。但是转换后会出现错误。

"Foutmelding": "Kan een object van het type System.String niet converteren naar het type System.Byte[]." “ Foutmelding”:“可用于对象的对象类型为System.String []。

In english that means 用英语这意味着

"Error message": "Can not convert a System.String object to the System.Byte [] type." “错误消息”:“无法将System.String对象转换为System.Byte []类型。”

The documentation is given here: 文档在这里给出:

doc link doc连结

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[]."
    }
}

To convert PDF to byte array you will have to read the document using file_get_contents() and then parse it by function unpack() . 要将PDF转换为字节数组,您将必须使用file_get_contents()阅读文档,然后通过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;
    }
?>

I think the problem is that WsdlToPhp has constructed client code which is sending the PDF entity as a String , when it needs to be a byte[] . 我认为问题在于WsdlToPhp已经构造了客户端代码,该客户端代码在需要为byte[]时将PDF实体作为String发送。

So I think the problem may be in code that is not shown here. 所以我认为问题可能出在这里未显示的代码中。

Can you take a look at the code generated by WsdlToPhp and see if you have any flexibility in how the $journaalpost is serialized to see if you can fix in there? 您可以看看WsdlToPhp生成的代码,看看在$journaalpost序列化方式上是否有任何灵活性,看看是否可以在其中进行修复?

// Convert the Base64 string back to text.
var byteString = atob(data.reportBase64Bytes);

// Convert that text into a byte array.
var ab = new ArrayBuffer(byteString.length);
var ia = new Uint8Array(ab);
for (var i = 0; i < byteString.length; i++) {
    ia[i] = byteString.charCodeAt(i);
}

// Blob for saving.
var blob = new Blob([ia], { type: "application/pdf" });

// Tell the browser to save as report.pdf.
saveAs(blob, "report.pdf");

// Alternatively, you could redirect to the blob to open it in the browser.
//document.location.href = window.URL.createObjectURL(blob);

There is a "bug" in the IMUIS software so it cant be possible to link a pdf file from php. IMUIS软件中存在“错误” ,因此无法从php链接pdf文件。 Its may be done by Dotnet because its native language and the entire system built by it. 它可能由Dotnet完成,因为它的母语和它所构建的整个系统。 Thanks to all of my SO friends for your efforts to helps me to find the issue, i have confirmed this information after call support. 感谢我所有的SO朋友为您帮助我发现问题所做的努力,在致电支持后,我已确认此信息。 So in the future no one get stuck (afterall maybe end of 2018) :) 所以将来没有人会卡住(毕竟可能在2018年底):)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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