ProSyn
September 26, 2024, 3:34pm
1
Using the following code to convert txt → docx
The saveAs request fails with the following message: Invalid model type a
I’m using the latest asposewordscloud
let saveRequest = new SaveAsRequest({
name: "translation.txt",
saveOptionsData: new DocxSaveOptionsData(
{
fileName: "translation.docx",
saveFormat: "docx"
})
});
let saveResult = await wordsApi.saveAs(saveRequest)
Please share your input document for analysis, as it seems to be a document-related issue.
ProSyn
September 26, 2024, 4:07pm
3
The text is generated - it is output from deepL translator.
We translate the text like this:
const result = await translator.translateText(text, null, targetLanguage as deepl.TargetLanguageCode);
Then we want to convert the result.text
to docx.
I tried to save the text to file and then convert it separately and it works. No idea on what is wrong.
ProSyn
September 26, 2024, 4:09pm
4
Here is the function we are using
export async function convertTextToDocument(text: string): Promise<Buffer> {
console.log("Converting text to document");
// upload file to the Aspose cloud
const textStream = Readable.from(text);
const uploadRequest = new UploadFileRequest();
uploadRequest.path = "translation.txt";
uploadRequest.fileContent = textStream;
const uploadResult = await wordsApi.uploadFile(uploadRequest);
if (uploadResult.response.statusCode !== 200) {
console.error("Error uploading file: " + uploadResult.response.statusMessage);
throw new Error("Error uploading file");
}
let saveRequest = new SaveAsRequest({
name: "translation.txt",
saveOptionsData: new DocxSaveOptionsData(
{
fileName: "translation.docx",
saveFormat: "docx"
})
});
let saveResult = await wordsApi.saveAs(saveRequest)
if (saveResult.response.statusCode !== 200) {
console.error("Error saving file: " + saveResult.response.statusMessage);
throw new Error("Error saving file");
}
const downloadRequest = new DownloadFileRequest({ path: "translation.docx" });
const downloadResult = await wordsApi.downloadFile(downloadRequest);
if (downloadResult.response.statusCode !== 200) {
console.error("Error downloading file: " + downloadResult.response.statusMessage);
throw new Error("Error downloading file");
}
return downloadResult.body;
}
Could you please try this method instead of saveas
return wordsApi.saveAsTiffOnline(request)
.then((resultApi) => {
// Assert
expect(resultApi.response.statusCode).to.equal(200);
});
});
});
// A test for ConvertDocument.
describe("convertDocument test", () => {
it("should return response with code 200", () => {
const wordsApi = BaseTest.initializeWordsApi();
const requestDocument = fs.createReadStream(BaseTest.localBaseTestDataFolder + localFolder + "/test_uploadfile.docx");
const request = new model.ConvertDocumentRequest({
document: requestDocument,
format: "pdf"
});
// Act
return wordsApi.convertDocument(request)
if the error persists with the convert document method, I will need the input document.
ProSyn
September 27, 2024, 11:45am
6
I think ConvertDocumentRequest
is for conversion from docx.
I’m attempting the opposite conversion, i.e. text → docx.
Anyway, the conversion fails only in my next.js app. If I extract the code and run it separately, it works. Trying to figure out, what is going out, but could not figure it out so far.
ProSyn
September 27, 2024, 12:09pm
7
I’m taking that back.
Using ConvertDocumentRequest
worked.
Glad to hear you were able to resolve the issue.