• Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
    Dedicated community for Japanese speakers
  • 한국 커뮤니티
    Dedicated community for Korean speakers

Unable to delete pdf file after pdf to docx conversion (in the same function call)

New Here ,
Sep 30, 2021 Sep 30, 2021

Copy link to clipboard

Copied

I have followed the code reference (ref: https://www.adobe.io/apis/documentcloud/dcsdk/pdf-services.html) needed for converting a PDF to DOCX, and that works as expected. However the same code also locks the PDF file and I am unable to delete the PDF file during the same function call.

 

I am attaching both the code that I have written as well as the error that I get on trying to delete the PDF file that is opened by

FileRef sourceFileRef = FileRef.CreateFromLocalFile(fullFilesPath + pdfFile);

 

I checked for a dispose method for any of the objects that are initiated from the Adobe Services API, but there are none, I have (if you see the screenshot named PDF to word code) also set all objects to null, but that also does nothing. Regardless there should either be a close method to close the PDF file that was opened or a dispose method to do the same.

 

Requesting a quick reply on this.

TOPICS
PDF Services API

Views

400

Likes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Adobe Employee ,
Oct 05, 2021 Oct 05, 2021

Copy link to clipboard

Copied

Can you talk a bit more about the environment your code is running in? I primarily use Node and I've not seen this, but in the past I used a Java application server (ColdFusion via Tomcat) and noticed that the Java server locked access to the file when running as the machine's system account. When I edited the Windows service to run as me, I had no problem deleting the file. Of course, that was _my_ access, my code had no issue deleting it. If you look at the file's properties, do you see anything unsual?

Likes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Oct 05, 2021 Oct 05, 2021

Copy link to clipboard

Copied

So I am on .Net 4.8 and my coding environment is VS 2015, also this is a MVC 4 project in which I am using the Adobe API.

 

The file is locked as long as the current function call exists (via the webbrowser request). In a consequent request I can delete the file thus indicating that code within the request, which I isolated to the Adobe API - the only new code I have written in that function is locking the file. Please note the code to delete the temporary PDF was always there, but after doing a PDF to word conversion started failing.

 

I can delete the file in the second call, but I don't have the file names and this is a client-server application hence there could be other requests also happening simultaneously, and hence can't just delete all files on server, rather at the time of function call, I would need to delete just the one temporary PDF file that I created.

Likes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Adobe Employee ,
Oct 06, 2021 Oct 06, 2021

Copy link to clipboard

Copied

So while it's definitely not the same, I'm not able to reproduce this in Node. Question - and forgive me for being ignorant of .NET, can you create scripts in .NET that run from the command line? And by that I mean, if you make a simple script that generates a file and immediately deletes it, and that works, then it confirms it's not a general .NET issue but an issue with our SDK in the environment of a web application. Does that make sense?

 

Also - and this is NOT a great solution but one to consider. If you store the PDFs in a directory, you could write a scheduled .NET service to run once an hour or so, list the files in the directory, and if the timestamp for the file is more than 60 minutes, delete them. This would keep the folder not too 'fat'. Again, I'm not saying this is the best solution, but perhaps a good temporary one.

Likes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Oct 27, 2021 Oct 27, 2021

Copy link to clipboard

Copied

I am on node v16.5.0 and I am facing the same issue. I am converting a PDF to .DOCX and .PPT file but it seems like it is holding on to the PDF file as when I delete the PDF file it still shows up in the system but as soon as I close the server I gets deleted. Pasting my code below for reference.

const getPDFConvertedToDOCXOrPPT = async (requestedFormat) => {
	console.info("getPDFConvertedToDOCXOrPPT is called");
	try {
		// Initial setup, create credentials instance.
		const credentials = PDFServicesSdk.Credentials.serviceAccountCredentialsBuilder()
			.fromFile("./../pdfservices-api-credentials.json")
			.build();

		//Create an ExecutionContext using credentials and create a new operation instance.
		const executionContext = PDFServicesSdk.ExecutionContext.create(credentials),
			exportPDF = PDFServicesSdk.ExportPDF;

		let format, savePath;
		if (requestedFormat === DOCX) {
			format = exportPDF.SupportedTargetFormats.DOCX;
			if (fs.existsSync("./../screenshot-designs/output/screenshot.docx")) {
				fs.unlinkSync("./../screenshot-designs/output/screenshot.docx");
			}
			savePath = "./../screenshot-designs/output/screenshot.docx";
		} else {
			if (fs.existsSync("./../screenshot-designs/output/screenshot.pptx")) {
				fs.unlinkSync("./../screenshot-designs/output/screenshot.pptx");
			}
			format = exportPDF.SupportedTargetFormats.PPTX;
			savePath = "./../screenshot-designs/output/screenshot.pptx";
		}
		const exportPdfOperation = exportPDF.Operation.createNew(format);

		// Set operation input from a source file
		const input = PDFServicesSdk.FileRef.createFromLocalFile("./../screenshot-designs/output/screenshot.pdf");
		exportPdfOperation.setInput(input);

		// Execute the operation and Save the result to the specified location.
		const result = await exportPdfOperation.execute(executionContext);
		return result.saveAsFile(savePath);
	} catch (err) {
		if (
			err instanceof PDFServicesSdk.Error.ServiceApiError ||
			err instanceof PDFServicesSdk.Error.ServiceUsageError
		)
			console.error("Error encountered from Adobe PDF SDK");

		console.log("Exception encountered while executing operation", err);
		return;
	}
};

Likes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Adobe Employee ,
Oct 27, 2021 Oct 27, 2021

Copy link to clipboard

Copied

When you say server - are you running something like Express? Any chance you can share a complete working example? 

 

Also, this may be your issue:

 

return result.saveAsFile(savePath);

 

saveAsFile returns a promise and nothing is sent to the resolved result. I'd at least await there.

Likes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Jul 16, 2022 Jul 16, 2022

Copy link to clipboard

Copied

LATEST

I have the same issue. I'm using coldfusion 2016 on a windows server with IIS. the same cffile delete command works fine seconds later. 

 

I'm just looking to either overwrite the input file with the output, or delete the input file and rename the output. In my case, the file name is in a database record and could be a web link we want to preserve, so we don't want to change the file name.

 

Error:

ColdFusion could not delete the file D:\www\com...

 

  <cffunction name="compress" access="remote" roles="admin,user,manager" hint="This query returns the data.">
    <cfset variables.document = getDocument(document_fix=2)>
    <!--- compress pdf---> 
    <cfoutput query="variables.document">
      <cfset variables.status = #document_fix#>
      <cftry> 
        <cfset variables.docDir = "#application.appdir##site_id#\documents">
        <cfset variables.docFile = "#document_path#">
        <cfset variables.docPath = "#docDir#\#docFile#">
        <cfset variables.source = createObject('java','com.adobe.pdfservices.operation.io.FileRef').createFromLocalFile(variables.docPath)>
        <cfset variables.destination = variables.docDir & "\COM." & variables.docFile>
        <cfset pdfOp = createObject('java', 'com.adobe.pdfservices.operation.pdfops.CompressPDFOperation').createNew()>
        <cfset pdfOp.setInput(source)>
        <cfset ev = auth()>
        <cfset result = pdfOp.execute(ex)>
        <cfif IsObject(result)>
          <cfset result.saveAs(variables.destination)>
          <cfif FileExists(variables.destination)>
            <cffile action="delete" file="#variables.docPath#">
            <cffile action="rename" source="#variables.destination#" destination="#variables.docPath#">
          </cfif>
        </cfif>
        
        <cfcatch>
          <cfset variables.status = 4>
        </cfcatch>
      </cftry>
      <cfset variables.updateDocument = updateDocument(document_id=#document_id#,document_fix=#variables.status#)>
    </cfoutput>
  </cffunction>

 

Likes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Resources