Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to access unoserver via curl? #162

Open
egrekov opened this issue Feb 3, 2025 · 9 comments
Open

How to access unoserver via curl? #162

egrekov opened this issue Feb 3, 2025 · 9 comments

Comments

@egrekov
Copy link

egrekov commented Feb 3, 2025

Maybe someone knows, as far as I understand unoconvert via REST accesses the unoserver and converts the document to PDF. Is it possible to convert a document via CURL without using unoconvert?

I am developing a REST application and would like to directly access unoserver.

@regebro
Copy link
Member

regebro commented Feb 3, 2025

The server communicates to the client with XMLRPC. You should, in theory, be able to use any XMLRPC client. But I'm not an expert on XMLRPC and internally both the server and client are using Python's XMLRPC library that deals with all the details so I don't have to. The protocol might also change between versions, so I would recommend continuing to use the client to do the conversions.

@egrekov
Copy link
Author

egrekov commented Feb 3, 2025

Thank you. It's a pity, it would be convenient to contact unoserver via curl, I'll poke around a little more, maybe something will come of it.

@strichter
Copy link
Collaborator

strichter commented Feb 3, 2025 via email

@regebro
Copy link
Member

regebro commented Feb 3, 2025

Using curl has absolutely no benefit over using the client. Using an XMLRPC library in the language you are programming in would make sense, although I won't recommend it. But using curl, absolutely not.

@egrekov
Copy link
Author

egrekov commented Feb 3, 2025

You need to use a method like this. You are right, I plan to use it to send a request from a PHP application, that's why I asked how to do it on curl, to test it.

curl -X POST http://127.0.0.1:2003 -H "Content-Type: text/xml" --data-binary @- <<EOF
<?xml version="1.0"?>
<methodCall>
    <methodName>convert</methodName>
    <params>
        <param>
            <value><nil/></value>
        </param>
        <param>
            <value>
                <value><string>base64</string></value>
            </value>
        </param>
        <param>
            <value><nil/></value>
        </param>
        <param>
            <value><string>3.pdf</string></value>
        </param>
        <param>
            <value><string>pdf</string></value>
        </param>
        <param>
            <value><nil/></value>
        </param>
        <param>
            <value><nil/></value>
        </param>
        <param>
            <value><boolean>false</boolean></value>
        </param>
        <param>
            <value><nil/></value>
        </param>
    </params>
</methodCall>
EOF

@regebro
Copy link
Member

regebro commented Feb 3, 2025

Yeah, that seems reasonable. If you look in server.py you can see what the parameters are for the convert methos. The info method will also give you an API version number, that I hope to remember to increase every time the API changes.

The client will always first call the info method to check that the server is started, and check that the API version is correct. I suggest you do that as well.

@egrekov
Copy link
Author

egrekov commented Feb 3, 2025

$file           = file_get_contents(Yii::getAlias('@console') . '/runtime/3.docx');
$outputFileName = Yii::getAlias('@console') . '/runtime/output.pdf';
$fileBase64     = base64_encode($file);

$client = new Client("http://unoserver:2003");
$req    = new Request('convert', [
    new Value(null, 'string'),
    new Value($fileBase64, 'base64'),
    new Value(null, 'null'),
    new Value('pdf', 'string'),
    new Value(null, 'null'),
    new Value([], 'array'),
    new Value(true, 'boolean'),
    new Value(null, 'null'),
]);

$response = $client->send($req);

if (!$response->faultCode()) {
    file_put_contents($outputFileName, $response->val->scalarVal());
}

I wrote this code, and I get a pdf as output, but it has a base64 string :) The truth is somewhere nearby, perhaps Python uses the rb mode to read the file, but PHP doesn't.

@egrekov
Copy link
Author

egrekov commented Feb 3, 2025

https://github.com/lynxtaa/unoserver-web

Here's the thing I found, it looks like it's what I need, well at least for the time being, until I figure out how to do it myself.

@regebro
Copy link
Member

regebro commented Feb 3, 2025

Well, that starts a webserver that will call the unoconvert client in a subprocess, which I assume you can do from PHP as well? So it's one step more than necessary.

But if it works for you, sure.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants