Create a transformer on share ZIP to PDF

cancel
Showing results for 
Search instead for 
Did you mean: 
Established Member II

Create a transformer on share ZIP to PDF

Jump to solution

Hi , i have zipped a single pdf file and convert them to zip file e.g. test.pdf to test.zip a container of the original file.

What i want it's to extract the single pdf in the zip and send to share like a normal pdf on runtime while using share, so i can use the standard preview e standard thumbnail of the pdf file.

===============================================================

/src/main/resources/alfresco/extension/mimetype/custom-mimetype-map.xml

===============================================================

<alfresco-config area="mimetype-map">
  <config evaluator="string-compare" condition="Mimetype Map">
    <mimetypes>
      <mimetype mimetype="application/octet-stream" display="ZIP">
        <extension>zip</extension>
      </mimetype>
    </mimetypes>
  </config>
</alfresco-config>

=================================================================

/src/main/resources/alfresco/module/webdesktop-amp/alfresco-global.properties

=================================================================

.......

#TRANSFORMER

content.transformer.ZIPToPDF.priority=100
content.transformer.ZIPToPDF.extensions.zip.pdf.supported=true

................

=================================================================

/src/main/resources/alfresco/module/webdesktop-amp/mimetype-map-custom.xml

=================================================================

<?xml version='1.0' encoding='UTF-8'?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
          http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">

       <!-- IS EMPTY -->
 </beans>

=================================================================

/src/main/resources/alfresco/module/webdesktop-amp/module-context.xml

=================================================================

<?xml version='1.0' encoding='UTF-8'?>
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">
    <import resource="classpath:alfresco/module/${project.artifactId}/context/*-context.xml"/>
</beans>

=================================================================

/src/main/resources/alfresco/module/webdesktop-amp/context/transformer-context.xml

=================================================================

<?xml version='1.0' encoding='UTF-8'?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
          http://www.springframework.org/schema/beans/spring-beans-3.0.xsd"> 
       <bean id="transformer.ZIPToPDF"
             class="it.abd.alfresco.transformers.ZIPToPDFTransformer"
             parent="baseContentTransformer" >                      
       </bean>
</beans>

=================================================================

/src/main/java/it/abd/alfresco/transformers/ZIPToPDFTransformer.java

=================================================================

import java.io.BufferedInputStream;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.io.Writer;
import java.net.URLConnection;
import java.nio.charset.StandardCharsets;

import org.alfresco.repo.content.transform.AbstractContentTransformer2;
import org.alfresco.repo.content.transform.UnsupportedTransformationException;
import org.alfresco.service.cmr.repository.ContentReader;
import org.alfresco.service.cmr.repository.ContentWriter;
import org.alfresco.service.cmr.repository.TransformationOptions;
import org.apache.commons.io.IOUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import it.abd.sign.lib.SignAlfrescoLib;

public class ZIPToPDFTransformer extends AbstractContentTransformer2
    {
    private static final Log logger = LogFactory.getLog(ZIPToPDFTransformer.class);
    
    
    /**
     * Select only the file to transform
     */
    public boolean isTransformable(String sourceMimetype, String targetMimetype, TransformationOptions options)
    {
        String ext = getExtensionOrAny("application/zip");    
        if ("application/zip".equalsIgnoreCase(sourceMimetype) || "application/octet-stream".equalsIgnoreCase(sourceMimetype) || ext.equalsIgnoreCase("zip")){
             logger.debug("ZIPToPDFTransformer IsTranformable '"+ sourceMimetype + "' to '" + targetMimetype+"'");
             return true;      
        }else{
             return false;
        }    
    }
        
    /**
     * Transofmer a zip to the pdf single file in it
     */
    @Override
    protected void transformInternal(ContentReader contentReader, ContentWriter contentWriter, TransformationOptions options) throws Exception
    {
        logger.debug("USE ZIPToPDFTransformer");
        BufferedReader reader = null;    
        Writer out = null;
        InputStream inStream = null;
        InputStream is = null;
        try
        {                   
            // Check the transformability
            String myMimeType = myStrictMimetypeCheck(contentReader, contentWriter, options);
            if(myMimeType !=null && !mimeType.isEmpty())contentWriter.setMimetype(myMimeType);
            //The zip file on share
            inStream = contentReader.getContentInputStream();
            out = new BufferedWriter(new OutputStreamWriter(contentWriter.getContentOutputStream()));
            //Extract Document pdf under the zip file (ignore these line they are work just fine)
            byte[] originalFile = SignAlfrescoLib.getOriginalDocumentBinaries(inStream);
            logger.debug("OriginalFile: " + String.valueOf(originalFile!=null && originalFile.length >0));
            is = new BufferedInputStream(SignAlfrescoLib.getOriginalDocumentStream(inStream));
            //Try to guess the mimetype of the document (ignore these line they are work just fine)
            String guessMimeType = URLConnection.guessContentTypeFromStream(is);
            logger.debug("GuessMimetype: " + guessMimeType);
            if(guessMimeType!=null){
                 contentWriter.setMimetype(guessMimeType);//this is "application/pdf"
            }
            //Copy the pdf file extract from zip file on the outputstream with a mimetype "application/pdf"
            //IOUtils.write(originalFile, outStream);
            IOUtils.write(originalFile, out,StandardCharsets.UTF_8.displayName());
            //outStream.flush();            
        }catch(IOException e){
            e.printStackTrace();
        }
        finally
        {
            if (reader != null)
            {
                try{reader.close(); }
                catch (Throwable e){e.printStackTrace();}
            }
            if (out != null) {
                try{out.close();}
                catch (Throwable e){e.printStackTrace();}
            }
            if(inStream != null){
                try{inStream.close();}
                catch (Throwable e){e.printStackTrace();}
            }
            if(is != null){
                try{is.close();}
                catch (Throwable e){e.printStackTrace();}
            }
        }
    }

    /**
     * Manage the inner warnings of alfresco 5.2
     * @param reader the reader of the current document
     * @param writer the writer of the current document
     * @param options the transform options of the current document
     * @return the mimetype suggested from share
     * @throws UnsupportedTransformationException
     */
    private String myStrictMimetypeCheck(ContentReader reader,ContentWriter writer, TransformationOptions options)throws UnsupportedTransformationException
    {
        String mimeTypeOfAlfresco = "";
        
        String sourceMimetype = reader.getMimetype();
        String targetMimetype = writer.getMimetype();     
        String differentType = getMimetypeService().getMimetypeIfNotMatches(reader.getReader());

        if (!transformerConfig.strictMimetypeCheck(sourceMimetype, differentType))
        {
            String fileName = transformerDebug.getFileName(options, true, 0);
            String readerSourceMimetype = reader.getMimetype();
            String message = "Transformation of ("+fileName+
                ") has not taken place because the declared mimetype ("+
                readerSourceMimetype+") does not match the detected mimetype ("+
                differentType+").";
            logger.warn(message);
            logger.debug("Cambiamo il mimetype con quello voluto da alfresco per evitare stackstrace del warning");
            mimeTypeOfAlfresco = differentType;
        }else{
            logger.debug("Mimetype ok : mimeTypeOfAlfresco = "+mimeTypeOfAlfresco+ " , sourceMimetype = "+sourceMimetype);
            mimeTypeOfAlfresco = sourceMimetype;
        }  
        return mimeTypeOfAlfresco;
    }
    
    
}

========================================================================================

The Alfresco version i use , is the 5.2 community.

Here the screenshot for make visualize the difference between a pdf file and a zip file on share:

I just want to retrieve the preview of the single document pdf in the zip file instead the fake preview of the zip file.

But i must do something wrong with the configuration of the transformer .

Hope i have made clear what i'm trying to do.

1 Solution

Accepted Solutions
Established Member II

Re: Create a transformer on share ZIP to PDF

Jump to solution

I for make the transformer i started with this addon:

GitHub - keensoft/alfresco-zip-previewer: Alfresco ZIP and RAR entries preview for Share 

and i simply customize the building of the document pdf .

View solution in original post

3 Replies
Master

Re: Create a transformer on share ZIP to PDF

Jump to solution

Why would you want to ZIP a single PDF file? If the sole reason is to optimise your content store disk usage, then that is the wrong way of doing it. Ideally, you would use transparent compression either on the filesystem level or by using a compressing content store.

With regards to the transformer: keep in mind that you also may need to configure the priority of your transformer to take precedence over the default ZIP transformer. By using the priority 100 you are simply using the default priority. Additionally, you may need to setup a composite / pipeline transformer for ZIP to PNG, since there is likely not going to be an existing one that works with PDF as an intermediary format (only text, which the default ZIP transformer provides).

Established Member II

Re: Create a transformer on share ZIP to PDF

Jump to solution

Hi ty for the response Faust, you are right on all your points , but my use case is particolar , i just use the example with the ZIP and PDF for making simple to explain my problem , in the real case my client has two need:

1) some archive (zip,ecc.) with many file on them (not just one) he want to have a preview of the content or at least a preview of the principal document instead the archive preview.

2) he use alfresco for many things, one of these is stored a bunch of signed document with CADES Signature, so he has many P7M file , the "zzz.p7m" is basicaly a zip file where i can retrieve the original content compressed in that, the client want on share (if you have the right permission but that is a different problem) , to have a preview of the original signed file, so if is a pdf a preview of the pdf, if is a text the preview of text on share.

It's acceptable even a image preview like the PNG, because these are file are not  been modify on the share application.

I will look to the project you link to me .

Any other suggestion for my problem is welcome.

Greetings.

Established Member II

Re: Create a transformer on share ZIP to PDF

Jump to solution

I for make the transformer i started with this addon:

GitHub - keensoft/alfresco-zip-previewer: Alfresco ZIP and RAR entries preview for Share 

and i simply customize the building of the document pdf .

View solution in original post