aspose-free-consulting / projects

The starting point for Aspose free consulting projects
5 stars 5 forks source link

Edit template word files for multiple contacts and convert all to pdf #397

Closed hash1pro closed 3 weeks ago

hash1pro commented 1 month ago

Please check if it satisfies the following requirements:

If your requirement is actually a bug in our APIs or a feature request for our APIs then please use our forum to report it and get quick help: https://forum.aspose.com/

If all the above requirements are satisfied then please move on:

Here is project to generate multiple pdf for different contacts, there will be one template file in word or pdf format and need to replace particular words in file and generate pdf file for each contacts.

Laravel 5.4 and php 7.1

AlexNosk commented 1 month ago

@hash1pro The best approach to achieve this is using mail merge feature. Please see our documentation for mode information: https://docs.aspose.com/words/net/types-of-mail-merge-operations/#simple-mail-merge-operation

hash1pro commented 1 month ago

yes but above things is for ASP.NET, i am using php also followed php example which using data file for mail merge but it did not work.

AlexNosk commented 1 month ago

@hash1pro The is no on-premises version of Aspose.Words for PHP. You can consider using Cloud version of Aspose.Words: https://docs.aspose.cloud/words/mail-merge/execute/

hash1pro commented 1 month ago

Yes i tried this but nothing happens, when i tried, it do not generate any file or do not link xml file with word document. Here how it generate multiple document and how i can convert all generated document in pdf? I can not see any clear instruction in how it generate multiple word file with mail merge. Can you give me your suggestion?

YaroslawEkimov commented 1 month ago

Yes i tried this but nothing happens, when i tried, it do not generate any file or do not link xml file with word document. Here how it generate multiple document and how i can convert all generated document in pdf? I can not see any clear instruction in how it generate multiple word file with mail merge. Can you give me your suggestion?

Could you please share your sample code? Also, please check https://github.com/aspose-words-cloud/aspose-words-cloud-php/blob/master/tests/Aspose/Words/BatchTests.php there, you can find an example of how to execute several operations at once.

hash1pro commented 1 month ago

ok here is my code, which used for mail merge,, here i can not find any file on https://dashboard.aspose.cloud/files, i tried other code also which given here https://github.com/aspose-words-cloud/aspose-words-cloud-php/blob/master/tests/Aspose/Words/ExecuteMailMergeTests.php which created same docx file on server but it is same file as my local no mail merge integration nothing. I think i am not going in proper way.

`$wordsApi = new WordsApi($clientId, $secret); $requestTemplate = storage_path('temp/MailMerge/SampleExecuteTemplate.docx'); $requestData = storage_path('temp/MailMerge/SampleExecuteTemplateData.xml');

    $request = new Requests\GetDocumentFieldNamesOnlineRequest(
        $requestTemplate,
        NULL,
        NULL,
        true
    );

    $result = $wordsApi->getDocumentFieldNamesOnline($request);
    Assert::isTrue(json_decode($result, true) !== NULL);
    Assert::assertNotNull($result->getFieldNames());
    Assert::assertNotNull($result->getFieldNames()->getNames());
    Assert::assertCount(15, $result->getFieldNames()->getNames());
    Assert::assertEquals("TableStart:Order", $result->getFieldNames()->getNames()[0]);`
YaroslawEkimov commented 1 month ago

You can find a mail merge example here https://github.com/aspose-words-cloud/aspose-words-cloud-php/blob/5cad76751c9f0c845d2bdae30a44b933fc514021/tests/Aspose/Words/ExecuteMailMergeTests.php#L67

or if you want the result document will be returned as a response, check another method https://github.com/aspose-words-cloud/aspose-words-cloud-php/blob/5cad76751c9f0c845d2bdae30a44b933fc514021/tests/Aspose/Words/ExecuteMailMergeTests.php#L43

hash1pro commented 1 month ago

Yes i tried this also which has created docx on aspose file report but it is same file and do not have any changes. So can you let me know process which steps i need to follow for mail merge and generate different pdf files from those mail merge files?

hash1pro commented 1 month ago

I have tried above things with curl as well as with php but nothing happens. I think i made some minor mistake but i can not catch it.

Here how i am doing this? I am working from my local used php 8.3 as well as 7.1 both. In my aspose app i used internal storage. When i run online method nothing happen. When i run only executeMailMerge it generate one docx file on live which is same as my local file. But it do not generate multiple file with merged data for me. Can you let me know how i can achieve this?

Thanks, Jay

YaroslawEkimov commented 1 month ago

Could you share your template and data, I'll come up with a solution, a bit later.

hash1pro commented 1 month ago

SampleExecuteTemplate.docx

He TestExecuteTemplateData.txt

Here are sample data i am using, i used online and other method mentioned in test file.

I tried individual block of xml also but it do not wokr.

Online do not showing any thing and other execute method showing result file online but without any change.

After mail merge i want to convert all to pdf file.

Thanks for your support.

Jay

YaroslawEkimov commented 4 weeks ago

SampleExecuteTemplate.docx

He TestExecuteTemplateData.txt

Here are sample data i am using, i used online and other method mentioned in test file.

I tried individual block of xml also but it do not wokr.

Online do not showing any thing and other execute method showing result file online but without any change.

After mail merge i want to convert all to pdf file.

Thanks for your support.

Jay

public function testMailMergeAndConvert()
    {
        $mailMergeFolder = "DocumentActions/MailMerge";
        $localDocumentFile = "SampleExecuteTemplate.docx";
        $localDataFile = "SampleExecuteTemplateData.txt";

        $requestTemplate = realpath(__DIR__ . '/../../..') . '/TestData/' . $mailMergeFolder . "/" . $localDocumentFile;
        $requestData = realpath(__DIR__ . '/../../..') . '/TestData/' . $mailMergeFolder . "/" . $localDataFile;
        $request = new ExecuteMailMergeOnlineRequest(
            $requestTemplate,
            $requestData,
            NULL,
            true,
            NULL,
            NULL
        );

        $result = $this->words->executeMailMergeOnline($request);

        $convertRequest = new ConvertDocumentRequest($result->getPathname(), "pdf");
        $convertedDocument = $this->words->convertDocument($convertRequest);
        file_put_contents(realpath(__DIR__ . '/../../..') . "/TestData/" . $mailMergeFolder . "/CovertedMailMergedFile.pdf", file_get_contents($convertedDocument->getPathname()));
        Assert::assertNotNull($result, "Error occurred");
        Assert::assertNotNull($convertedDocument, "Error occurred");
    } 

Here is an example based on an online mail merge from our SDK. I am answering your question about why nothing changed in the document after the mail merge. It is because, in the template, there is a table, which means you have to set the with_region parameter to true. We will update the tests so that they are clear. I hope it helps.

hash1pro commented 4 weeks ago

Hi,

This is really helpful, i appreciate you help on this. For me now it generate copy of pdf with mail merge data. Currently it is showing all mail merge data in single pdf, is there any way it can generate multiple pdf or it gives multiple doc file url for each mail merge data?

Or can we use any other data types like json instead of xml format? so in that case we can perform this for individual file with individual data. or if we can send direct data instead of file then it also good as we use database to store data so we can directly send data.

If you know source of ips aspose use to send information back so we can make it white list so our server will not block this.

Thanks, Jay

YaroslawEkimov commented 4 weeks ago

Hi,

This is really helpful, i appreciate you help on this. For me now it generate copy of pdf with mail merge data. Currently it is showing all mail merge data in single pdf, is there any way it can generate multiple pdf or it gives multiple doc file url for each mail merge data?

Or can we use any other data types like json instead of xml format? so in that case we can perform this for individual file with individual data. or if we can send direct data instead of file then it also good as we use database to store data so we can directly send data.

If you know source of ips aspose use to send information back so we can make it white list so our server will not block this.

Thanks, Jay

MailMerge operation doesn't work this way. You have a template, and you have data for this template; after you execute mail merge, the data you have is merged to the template document, so instead of template syntax, you have real data. I do not quite understand your use case. How do you want to get multiple doc files from one template and one dataset?

hash1pro commented 4 weeks ago

Hi actually i want to generate invoices for different users from one template file. So there will be different data for different users. So 1st i will execute mail merge and generate pdf. so in single process i can generate multiple files then it will be good. If it can not possible i can generate different file for each contacts but for that i need to convert each data into xml and save file and use that for mail merge so instead of if it can accept json or xml string directly then i directly can fire loop of array and can generate pdf for each contacts. Here you can see my current code, if i can skip temp xml generation process then also it will work for me.

`// Your PHP array $data = array( array( 'FullName' => 'John Doe', 'Company' => 30, 'Address' => 'USA' ), array( 'FullName' => 'Jay oza', 'Company' => 39, 'Address' => 'India' ), array( 'FullName' => 'Reyansh oza', 'Company' => 6, 'Address' => 'India' ) ); $clientId = "8351e0d8-d4b9-489c-8605-4198b2b568e9"; $secret = "2acf29f35369a8dd7b23a84ee9ec71eb"; $mailMergeFolder = storage_path('temp/MailMerge');

    $wordsApi = new WordsApi($clientId, $secret);

    //$mailMergeFolder = "DocumentActions/MailMerge";
    $localDocumentFile = "SampleMailMergeTemplate.docx";
    $localDataFile = "SampleExecuteTemplateData.txt";

    $requestTemplate = $mailMergeFolder . "/" . $localDocumentFile;
    $requestData =  $mailMergeFolder . "/" . $localDataFile;

    $i=0;
    foreach($data as $d){
        $xml = new \SimpleXMLElement('<Fields/>');
        $this->array_to_xml($d, $xml);
        $temp_file = tempnam(sys_get_temp_dir(), 'xml');
        $xml->asXML($temp_file);

        $request = new ExecuteMailMergeOnlineRequest(
            $requestTemplate,
            $temp_file,
            NULL,
            NULL,
            NULL,
            NULL
        );

        $result = $wordsApi->executeMailMergeOnline($request);

        $convertRequest = new ConvertDocumentRequest($result->getPathname(), "pdf");
        $convertedDocument = $wordsApi->convertDocument($convertRequest);
        file_put_contents($mailMergeFolder . "/CovertedMailMergedFile".$i.".pdf", file_get_contents($convertedDocument->getPathname()));

        $i++;
    }`
YaroslawEkimov commented 4 weeks ago

MailMerge accepts XML and JSON data, so you must serialize your data to send it via SDK.

hash1pro commented 4 weeks ago

Hi,

When i use ExecuteMailMergeOnlineRequest it only accept xml data in file format, when i use direct data then it gives fopen error. So can i use to send direct data here in json format by changing library format?

Wile ExecuteMailMergeRequest method (Without Online) accept data.

Thanks, Jay

YaroslawEkimov commented 4 weeks ago

TestExecuteTemplateData.txt here you can find the same data for template but in JSON format you can pass it to api method from your previous code sample and it will work.

hash1pro commented 3 weeks ago

Hi Thank you for your help, it means a lot to me. I have few question regarding docker image. if we purchase docker package and install that on our server then also will above code works? If we purchase docker image and setup that on our server do we still need to pay for per api call? or we can use docker to generate file as many of times without giving any extra fees? Please give me some details on docker concept. My client willing to purchase it for words api.

Thanks, Jay

YaroslawEkimov commented 3 weeks ago

Hi Thank you for your help, it means a lot to me. I have few question regarding docker image. if we purchase docker package and install that on our server then also will above code works? If we purchase docker image and setup that on our server do we still need to pay for per api call? or we can use docker to generate file as many of times without giving any extra fees? Please give me some details on docker concept. My client willing to purchase it for words api.

Thanks, Jay

Thanks for your query here you can find everything about the docker version https://purchase.aspose.cloud/self-hosting/

hash1pro commented 3 weeks ago

ok thank you got that.

hash1pro commented 5 days ago

Hi

I hope you are doing well, We have setup cloud php sdk on our server now when i am trying it is giving me this error, can you suggest how we can resolve this?

Server error: POST http://127.0.0.1:8082/connect/token resulted in a 500 Internal Server Error response: { "Error": { "Message": "Unable to resolve service for type 'Aspose.Words.Cloud.WebApp.Auth.IUserAuthenticationSer (truncated...)

Thanks, Jay