If you want to set one more custom tab to show pdf & download them then follow the below steps.
Step 1: Create a file like app/code/Magemonkeys/Custompdftab/registration.php
<?php
MagentoFrameworkComponentComponentRegistrar::register(
MagentoFrameworkComponentComponentRegistrar::MODULE,
'Magemonkeys_Custompdftab',
__DIR__
);
?>
Step 2: Create a file like app/code/Magemonkeys/Custompdftab/etc/module.xml
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Module/etc/module.xsd">
<module name="Magemonkeys_Custompdftab" setup_version="1.0.0">
<sequence>
<module name="Magento_Catalog"/>
</sequence>
</module>
</config>
Step 3: Create a file like app/code/Magemonkeys/Custompdftab/Helper/Data.php
<?php
namespace MagemonkeysCustompdftabHelper;
use MagentoFrameworkAppHelperAbstractHelper;
use MagentoFrameworkAppHelperContext;
class Data extends AbstractHelper
{
public function stringToTable($text, $colSeparator = '|', $rowSeparator = '||')
{
$rows = explode($rowSeparator, $text);
if (!$rows) return false;
$html = '<table>';
foreach ($rows as $row) {
if ($row) {
$html .= '<tr>';
$columns = explode($colSeparator, $row);
$w=count($columns);
foreach ($columns as $column) {
$html .= '<td style="width:'. 100/$w .'%">' . $column . '</td>';
}
$html .= '</tr>';
}
}
$html .= '</table>';
return $html;
}
public function applicationsRender($applications, $separator = "||"){
//echo 'called';exit;
if(!$applications)
return;
$html = '';
$_elements = '';
if(strpos($applications,':') > 0) {
$_items = explode('.',$applications);
natcasesort($_items);
foreach($_items as $_item){
$_item = explode(':',$_item);
if($_item[0] == '')
continue;
$html .= '<ul class="applications">';
$html .= trim($_item[0]).': ';
$_elements = @explode($separator,$_item[1]);
foreach ($_elements as $element){
$html .= '<li>'.trim($element).'</li>';
}
$html .= '</ul>';
}
} else {
$html .= '<ul class="applications">';
$_items = explode($separator,$applications);
natcasesort($_items);
foreach ($_items as $_item){
$html .= '<li>'.$_item.'</li>';
}
$html .= '</ul>';
}
return $html;
}
}
Step 4: Create a file like app/code/Magemonkeys/Custompdftab/view/frontend/layout/catalog_product_view.xml
<?xml version="1.0"?>
<page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd">
<body>
<referenceBlock name="product.info.details">
<block class="MagentoCatalogBlockProductView" name="custom-tab" template="Magemonkeys_Custompdftab::product/view/details/custom_tab.phtml" group="detailed_info">
<arguments>
<argument name="css_class" xsi:type="string">newtabclass</argument>
<argument translate="true" name="title" xsi:type="string">Documentatie</argument>
<argument name="sort_order" xsi:type="string">60</argument>
</arguments>
</block>
</referenceBlock>
</body>
</page>
Step 5 : Create file like app/code/Magemonkeys/Custompdftab/view/frontend/templates/product/view/details/custom_tab.phtml
<?php
$_product = $block->getProduct();
$sku = $_product->getSku();
$file = $_product->getAgreementFile();
$objectManager = MagentoFrameworkAppObjectManager::getInstance();
$fileSystem = $objectManager->create('MagentoFrameworkFilesystem');
$mediaPath = $fileSystem->getDirectoryRead(MagentoFrameworkAppFilesystemDirectoryList::MEDIA)->getAbsolutePath();
$dir = '/'.$mediaPath.'/'.'catalog/product/file/'.$file;
$dir2 = '/'.$mediaPath.'/'.'file/'.$sku;
$flag = FALSE;
$flag1 = true;
?>
<?php if(file_exists($dir) && $file != ''):{
$flag1 = false;
if($file){ $flag = TRUE; } ?>
<a class="download-file" href="<?php echo $block->getBaseUrl(); ?>pub/media/catalog/product/file/<?php echo basename($file) ?>" target="_blank"><?php echo str_replace('.pdf', '', $file) ?></a>
<?php } elseif(file_exists($dir2) && $flag1):{
if ($dh = opendir($dir2)){
while (($file = readdir($dh)) !== false){
if ($file != '.' && $file != '..') {
if($file){ $flag = TRUE; }?>
<a class="download-file1" href="<?php echo $block->getBaseUrl(); ?>pub/media/file/<?php echo $sku.'/'.basename($file) ?>" target="_blank"><?php echo str_replace('.pdf', '', $file) ?></a>
<?php
}
}
}
}
?>
<?php else: ?>
<?php echo "There is no file"; ?>
<?php endif; ?>
<?php if($flag == TRUE): ?>
<script>
require(['jquery', 'jquery/ui'], function($){
$('#tab-label-custom-tab').css('display','block');
});
</script>
<?php endif; ?>
That’s it…
Now, you can check your product detail page. Your custom tab will be showing after all default tabs.

