Translate pdf with Gravity pdf

Since the version 3.7 of Weglot plugin, you can translate pdf generate by Gravity PDF.

Below on example of how you can translate your PDF on the current language base on the gravity pdf filter here : https://docs.gravitypdf.com/v6/developers/filters/gfpdf_pdf_html_output/

// Add this using plugin "Code Snippet"

// create a new meta entry to store the weglot_language for the Gravity form entries
function get_entry_meta( $entry_meta, $form_id ) {
    $entry_meta['weglot_language']   = array(
        'label'                      => 'Weglot language',
        'is_numeric'                 => false,
        'is_default_column'          => true,
        'update_entry_meta_callback' => array( $this, 'update_entry_meta' ),
                );
    return $entry_meta;
}

function update_entry_meta( $key, $lead, $form ) {
    return ""; // return the value of the entry meta
}

//use the gform_after_submission hook to store the weglot language
add_action( 'gform_after_submission', 'after_submission' );
function after_submission( $entry ) {
	$request_url_services = weglot_get_service( 'Request_Url_Service_Weglot' );
	$current_language = $request_url_services->get_current_language()->getExternalCode();
    gform_update_meta( $entry['id'], 'weglot_language', $current_language );
}

// get the meta entries to translate the pdf
add_filter( 'gfpdf_pdf_html_output', function( $html, $form, $entry, $settings, $Helper_PDF ) {
	$qp = new GFPDF\Helper\Helper_QueryPath();
    $wrapper = $qp->html5( $html );
    $pdf_translate_service = weglot_get_service( 'Pdf_Translate_Service_Weglot' );
	$weglot_language = gform_get_meta( $entry['id'], 'weglot_language' );
	$html = $pdf_translate_service->translate_pdf( $html, $weglot_language );
	$wrapper = $qp->html5( $html['content'] );
    return $wrapper->top( 'html' )->innerHTML5();

}, 10, 5 );

Last updated