Ejemplo n.º 1
0
		void FileCreator(const std::string path,
						const std::string destFileName,
						const std::string outline ) {
			std::vector<std::string> files = util::listDir(path);
			if (files.size() <= 0)return;
			std::sort(std::begin(files), std::end(files));
			///////////////////////////
			HPDF_Doc  pdf;
			pdf = HPDF_New(error_handler, NULL);
			if (!pdf) {
				std::cout<<"error: cannot create PdfDoc object"<<std::endl;
				return;
			}

			if (setjmp(env)) {
				HPDF_Free(pdf);
				return;
			}

			/*
			const char *font_name1;
			const char *font_name2;
			const char *font_name3;
			font_name1 = HPDF_LoadTTFontFromFile(pdf, "C:\\Windows\\Fonts\\Arial.ttf", HPDF_TRUE);
			font_name2 = HPDF_LoadTTFontFromFile(pdf, R"(C:\Windows\Fonts\simsunb.ttf)", HPDF_TRUE);
			font_name3 = HPDF_LoadTTFontFromFile2(pdf, "C:\\Windows\\Fonts\\simsun.ttc", 1, HPDF_TRUE);
			*/

			HPDF_Outline root;
			root = HPDF_CreateOutline(pdf, NULL, outline.c_str(), NULL);
			HPDF_Outline_SetOpened(root, HPDF_TRUE);
			HPDF_SetInfoAttr(pdf, HPDF_INFO_AUTHOR, config::AUTHOR);
			HPDF_SetInfoAttr(pdf, HPDF_INFO_TITLE, path.c_str());

			HPDF_Font font;
			switch (config::LANG_TYPE)
			{
			case pdf::config::CN:
				HPDF_UseCNSFonts(pdf);
				HPDF_UseCNSEncodings(pdf);
				font = HPDF_GetFont(pdf, "SimHei", "GB-EUC-H");  // SimSun  SimHei
				break;
			case pdf::config::TW:
				HPDF_UseCNTFonts(pdf);
				HPDF_UseCNTEncodings(pdf);
				font = HPDF_GetFont(pdf, "MingLiU", "ETen-B5-H");
				break;
			default:
				font = HPDF_GetFont(pdf, "Helvetica", NULL);
				break;
			}

			///////////////////////////			
			std::string contents;
			std::vector<std::string> vec;
			for (std::string file : files) {
				contents = util::getFileContents(file);
				vec = util::split(contents, '\n');
				fileWriter(pdf, root, font, file.substr(path.length()), vec);
			}
			///////////////////////////			
			std::string destFile(path);
			destFile.append("/");
			destFile.append(destFileName);
			HPDF_SaveToFile(pdf, destFile.c_str());
			HPDF_Free(pdf);
		}//end FileCreator()
Ejemplo n.º 2
0
int main (int argc, char **argv)
{
    HPDF_Doc  pdf;
    char fname[256];
    HPDF_Font font;
    const char *font_name;
    int i = 0;
    HPDF_Outline root;

    const char *encodings[] = {
            "StandardEncoding",
            "MacRomanEncoding",
            "WinAnsiEncoding",
            "ISO8859-2",
            "ISO8859-3",
            "ISO8859-4",
            "ISO8859-5",
            "ISO8859-9",
            "ISO8859-10",
            "ISO8859-13",
            "ISO8859-14",
            "ISO8859-15",
            "ISO8859-16",
            "CP1250",
            "CP1251",
            "CP1252",
            "CP1254",
            "CP1257",
            "KOI8-R",
            "Symbol-Set",
            "ZapfDingbats-Set",
            NULL
    };

    pdf = HPDF_NewEx (error_handler, NULL, NULL, 0, NULL);
    if (!pdf) {
        printf ("error: cannot create PdfDoc object\n");
        return 1;
    }

    if (setjmp(env)) {
        HPDF_Free (pdf);
        return 1;
    }

    strcpy (fname, argv[0]);
    strcat (fname, ".pdf");

    /* set compression mode */
    HPDF_SetCompressionMode (pdf, HPDF_COMP_ALL);

    /* Set page mode to use outlines. */
    HPDF_SetPageMode(pdf, HPDF_PAGE_MODE_USE_OUTLINE);

    /* get default font */
    font = HPDF_GetFont (pdf, "Helvetica", NULL);

    /* load font object */
    #ifdef __WIN32__
    font_name = HPDF_LoadType1FontFromFile (pdf, "type1\\a010013l.afm",
            "type1\\a010013l.pfb");
    #else
    font_name = HPDF_LoadType1FontFromFile (pdf, "type1/a010013l.afm",
            "type1/a010013l.pfb");
    #endif

    /* create outline root. */
    root = HPDF_CreateOutline (pdf, NULL, "Encoding list", NULL);
    HPDF_Outline_SetOpened (root, HPDF_TRUE);

    while (encodings[i]) {
        HPDF_Page page = HPDF_AddPage (pdf);
        HPDF_Outline outline;
        HPDF_Destination dst;
        HPDF_Font font2;

        HPDF_Page_SetWidth (page, PAGE_WIDTH);
        HPDF_Page_SetHeight (page, PAGE_HEIGHT);

        outline = HPDF_CreateOutline (pdf, root, encodings[i], NULL);
        dst = HPDF_Page_CreateDestination (page);
        HPDF_Destination_SetXYZ(dst, 0, HPDF_Page_GetHeight(page), 1);
        /* HPDF_Destination_SetFitB(dst); */
        HPDF_Outline_SetDestination(outline, dst);

        HPDF_Page_SetFontAndSize (page, font, 15);
        draw_graph (page);

        HPDF_Page_BeginText (page);
        HPDF_Page_SetFontAndSize (page, font, 20);
        HPDF_Page_MoveTextPos (page, 40, PAGE_HEIGHT - 50);
        HPDF_Page_ShowText (page, encodings[i]);
        HPDF_Page_ShowText (page, " Encoding");
        HPDF_Page_EndText (page);

        if (strcmp (encodings[i], "Symbol-Set") == 0)
            font2 = HPDF_GetFont (pdf, "Symbol", NULL);
        else if (strcmp (encodings[i], "ZapfDingbats-Set") == 0)
            font2 = HPDF_GetFont (pdf, "ZapfDingbats", NULL);
        else
            font2 = HPDF_GetFont (pdf, font_name, encodings[i]);

        HPDF_Page_SetFontAndSize (page, font2, 14);
        draw_fonts (page);

        i++;
    }

    /* save the document to a file */
    HPDF_SaveToFile (pdf, fname);

    /* clean up */
    HPDF_Free (pdf);

    return 0;
}